simple example of bit manipulation in python?

Hans Nowak hnowak at cuci.nl
Thu Aug 16 06:34:43 EDT 2001


>===== Original Message From "Thomas Weholt" <thomas at gatsoft.no> =====
>Hi,
>
>Can anobody point to or provide a simple example of bit manipulation using
>plain python?

Not sure what you want here... You can use the & ^ | >> << operators, like in 
C. A trivial example:

>>> a = 2001
>>> a & 1973
1937
>>> a | 1973
2037
>>> a ^ 1973
100
>>> a >> 1
1000
>>> a << 1
4002

If you meant something more sophisticated, like high-level functions to set 
the Nth bit of a number, or working with binary numbers, then I might have 
something in my snippet collection. I can look it up for you if you want.

HTH,

--Hans Nowak





More information about the Python-list mailing list