bit operations with python?

Terry Reedy tjreedy at udel.edu
Fri Nov 5 06:44:59 CET 2004


"Erik Max Francis" <max at alcyone.com> wrote in message 
news:418AF094.200B96C4 at alcyone.com...
> Jason wrote:
>
>> Is it possible to do this sort of thing with python? If so, could
>> someone recommend a website with instructions on bit operations?
>> Thanks!
>
> Yes.  The operators in Python are the same as described by the
> challenge.  Bitwise and is &, bitwise or is |, and bitwise xor is ^.

In C, chars *are* ints (of size 1).  For Python, you need to convert the 
bytes to ints with ord(), do the bit manipulations, and convert back to 
bytes with chr().  Make a list of bytes and ''.join(them) at the end.

And/or, you might want to use the array module.

Terry J. Reedy






More information about the Python-list mailing list