manipulating bits in python
Rainer Deyke
root at rainerdeyke.com
Sun Oct 1 00:23:46 EDT 2000
"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?
Catch it before you do the shift:
bit_that_fell_off = number_to_shift & 1
number_to_shift = number_to_shift >> 1
> 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
To get the full (32 bit or more) inverse, use the ~ operator. To get the
byte size inverse, use ^ 0xff.
full_inverse = ~byte_to_invert
byte_size_inverse = byte_to_invert ^ 0xff
--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor
More information about the Python-list
mailing list