[SciPy-user] Manipulating Bit-Masked Data

Travis Oliphant oliphant.travis at ieee.org
Tue Dec 27 22:17:09 CST 2005

Rich Shepard wrote:

>   I need to read bit-masked bytes from an Optical Mark Recognition card
>reader. I'm quite new to Python and haven't found any documentation on how to
>manipulate bits. NumPy has masked arrays, but that's a different situation.
>My rummaging around the SciPy wiki and a search of the mail list archives
>turned up nothing.
>   I'd appreciate a pointer to documentation that will show me how to
>manipulate bit-masks in Python.
There are not intrinsic bit-fields in scipy, but you can easily access 
bits of interest using an integer type, bit-masks, and the bit-operators.

 >> (right-shift)
<< (left-shift)
& (bit-wise and)
| (bit-wise or)
^ (bit-wise xor)


bit0 = 1
bit1 = 2
bit2 = 4
bit3 = 8
bit4 = 16
bit5 = 32
bit6 = 64
bit7 = 128

would give you bit masks you could check using

var & bitn

Perhaps you could show an example of what you would like to do.  I'd 
love to give a more specific response.



More information about the SciPy-user mailing list