[Tutor] working with bit arrays

Kent Johnson kent37 at tds.net
Wed Dec 2 20:30:14 CET 2009


On Wed, Dec 2, 2009 at 1:08 PM, Robert Berman <bermanrl at cfl.rr.com> wrote:
> Hi,
>
> I am trying to represent a number as a list of bits: for example the bit
> representation of the integer 8. I did find a number of articles pertaining
> to a module called bitarray but I was unable to download/install that
> package. I am using Linux on Ubuntu 9.10; Python 2.6.2.
>
> I am almost certain there is a relatively easy way to convert an integer
> that can be represented by 32 bits into an array of bits that I can iterate
> over looking for switched on bits or switched off bits.

If all you want to do is test bits, you can do that directly using
bit-wise logical operators & and |. There is no need to convert to a
different representation. For example

In [1]: 0xff & 4
Out[1]: 4

In [2]: 0xf0 & 4
Out[2]: 0

Kent


More information about the Tutor mailing list