Ask Your Question

Revision history [back]

The bitwise NOT operator in Python is "~". It is a unary operator that takes one operand and inverts all the bits of it. It treats the operand as a binary number and flips all the 0s to 1s and vice versa, including the sign bit which affects the value.

For example, "~0b0101" (binary representation of decimal 5) gives "-0b0110" (binary representation of decimal -6) because it flips all the bits and adds a negative sign to the result due to the sign bit flip. Similarly, "~0b11111111" gives "-0b100000000" because it flips all the 1s to 0s and carries over a 1 to the next bit, resulting in a negative number.

Note that when using the bitwise NOT operator with integers, it operates on the binary representation of the number, not the decimal representation.