adafruit / Adafruit-Raspberry-Pi-Python-Code

Adafruit library code for Raspberry Pi
1.43k stars 686 forks source link

What is the point of masking an 8 bit register value with 0xFF? #87

Closed asheeshr closed 10 years ago

asheeshr commented 10 years ago

In Adafruit_ADS1x15.py, at multiple places this bitwise manipulation is performed.

(result[1] & 0xFF)

Which is equivalent to:

(result[1] & 0b11111111)

What is the point of this operation? Wont this always evaluate to result[1] and is therefore unnecessary?

tdicola commented 10 years ago

It's just to be safe, since Python doesn't have strict types you could get a numeric value greater than 8 bits which will usually fail when I2C, SPI, etc. functions interpret the data. The bitwise and with 0xFF just ensures only the lower 8 bits of the value are used. It's not strictly required in a lot of spots but can't hurt to have as a precaution.