Friday, April 4, 2014

Python bitwise not operator

I had a strangely difficult time finding information about the bitwise not operator in Python.
The operator is the tilde (~) character, but since Python only uses signed integers (right?) you must and the result to mask off the unwanted portion.

In [1]: ~15
Out[1]: -16
In [2]: ~15 & 0xff
Out[2]: 240

In [3]: ~240 & 0xff
Out[3]: 15