Operators

~ — Bitwise inversion

For an integer n, computes -n-1, corresponding to inversion with conceptually unbounded sign extension. To invert a fixed-width bit field, mask the result with the desired width. Inverting a bool is deprecated since Python 3.12; use not for Boolean negation. Custom objects can define __invert__. This is a unary operation, not a logical truth test.

Formal Syntax & Structure

python syntaxpython
~operand

Parameters & Attributes

NameTypeDescription
operandint or supporting objectValue to transform.

Return Value

object

Result selected by the operand types and their operation implementations.

Working Example

Example Snippetpython
print(~5)print(~0)print(format(~0b1010 & 0b1111, "04b"))
Python Reference