Bitwise operations on Python ints work much like in C. The &
, |
and ^
operators in Python work just like in C. The ~
operator works as for a signed integer in C; that is, ~x
computes -x-1
.
You have to be somewhat careful with left shifts, since Python integers aren't fixed-width. Use bit masks to obtain the low order bits. For example, to do the equivalent of shift of a 32-bit integer do (x << 5) & 0xffffffff
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…