manipulating bits in python

Greg Jorgensen gregj at pobox.com
Sun Oct 1 00:40:08 EDT 2000


Python has a full set of bitwise operators, which can be applied (only) to
integers and long integers:

x << y    left shift x by y bits
x >> y    right shift
x & y    bitwise and
x | y    bitwise or
x ^ y    bitwise xor
~x    bitwise negation

print "%x" % a    # print a as hex integer

I recommend the excellent book "Python Essential Reference" by David
Beazley.

Greg Jorgensen
Deschooling Society
gregj at pobox.com
Reading, because life's too short for buggy people.


"Jim Richardson" <warlock at eskimo.com> wrote in message
news:slrn8tckiq.h5.warlock at gargoyle.myth...
> I have a ??? re: manipulating bits in python. I have a need to do quick
and
> simple bit twiddling, shiftright, invert etc. THe operator module has s
shiftR
> but how can I catch the bit that falls off without a lot of grotty_code?
>  Also, I can't seem to figure a "invert" number, something that would take
> for example, 0x01 and return 0xFE, I can do this fairly simply, but am
confused
> at not finding a simple call for it. Am I missing something? thanks all
>
> --
> Jim Richardson
> Anarchist, pagan and proud of it
> WWW.eskimo.com/~warlock
> Linux, because life's too short for a buggy OS.
>





More information about the Python-list mailing list