Image

Bit Bashing

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?