[Tutor] unsigned int or char equivalent in Python?

Peter Otten __peter__ at web.de
Tue Feb 4 16:01:16 CET 2014


Alan Gauld wrote:

> I'm playing with some bit twiddling code at present and started
> to get some unexpected results which i eventually realized
> were down to Python ints acting like signed ints in C (but
> with a 'flexible' sign bit!)
> 
> This led me to wonder if there is a way to get unsigned type
> behaviour in Python?
> 
> This could be very useful in bit twiddling scenarios, especially
> now that Python is being used on RasberryPi type devices to
> control Arduinos and the like. I had a quick hunt in the docs
> but couldn't think of a way to fake it.

I think you have to apply a mask:

>>> -2 & 0xff
254




More information about the Tutor mailing list