previous next Up Title Contents Index

Bitwise operation


Summary

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
- Greg Jorgensen

how can I catch the bit that falls off a rshift ?

Catch it before you do the shift:

bit_that_fell_off = number_to_shift & 1
number_to_shift = number_to_shift >> 1

- Rainer Deyke

invert number ?

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


previous next Up Title Contents Index

Version : 1.65 Mise à jour : 11/11/00