2

I am making an TF2 backpack viewer in Python, and I have inventory token that is an 32 unsigned long. First 16 bits are unimportant for me. Usual approach in C would be something like

(a<<16)>>16

to get last 16 bits. But Python is no C, and it above operation will not work. How do I specify that Python SHOULD use int32 for this variable?

RomaValcer
  • 2,786
  • 4
  • 19
  • 29

2 Answers2

4

You can use bitwise AND operator (&):

>>> 0x12345678 & 0xffff
22136
>>> hex(_)
'0x5678'
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • @RomaValcer, See [`Bitwise operation - AND` wikipedia page](http://en.wikipedia.org/wiki/Bitwise_operation#AND) – falsetru Jan 09 '14 at 08:07
0

You may use array

array.array('H', [10])

Will create array of 1 unsigned short word. (Several years ago I've written a HW driver in Python combining array and struct

volcano
  • 3,578
  • 21
  • 28