Top.Mail.Ru
Python
? ?
LiveJournal for python.
View:Personal Journal.
View:Friends.
View:Calendar.
Missed some entries? Then simply jump to the previous day or the next day.

Tuesday, February 14th, 2006

Subject:Bit Bashing
Time:11:03 am.
How do I force Python to interpret a number as a two's complement negative?
Let me give you the background:
I'm reading and writing a bunch of data over a serial port using the pyserial module. As the data is of arbitrary length I'm reading bytes into an array('B') untill I get a full packet, and then later interpreting the packet.

My problem is that at some point I am reading in a 4 byte long int. As long as the int is positive everything is fine, but if it is negative, say 0xFFFFFFFF I get 4294967295 instead of -1.

The code that is producing this looks like:
self.AzNegLimitTxt.SetValue(str(data[3] << 24 | data[4] << 16 | data[5] << 8 | data[6]))


Any ideas?
Comments: Read 11 orAdd Your Own.

LiveJournal for python.

View:User Info.
View:Friends.
View:Calendar.
View:Memories.
Missed some entries? Then simply jump to the previous day or the next day.

Image