Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Operator ~

Inverts every bit of a Python integer.

Version: Python 3.14; repository image, production runtime not attested

Syntax

~integer

Inverts every bit of a Python integer.

Integer inversion behaves as if negative numbers had infinitely many sign bits. To restrict the result to one byte, combine it with a mask whose low eight bits are set.

This is a unary operation, so there is no reflected second operand. Floating-point values do not implement integer bit inversion. Inverting a Boolean is deprecated since Python 3.12, still supported in 3.14, and scheduled to raise an error in 3.16.

The result for any integer is its arithmetic negation minus one.

Syntax

~integer

__invert__ · TypeError

Worked example

~x == -x - 1   (int)
~0 → -1
(~0b0101) & 0b1111 → 0b1010

Examples

print(~5)
print(~(-1))

Result

-6
0

The result for any integer is its arithmetic negation minus one.

Sources