Bitwise NOT

Flips every bit: 0 becomes 1 and vice versa. On a fixed width, ~a equals (2ⁿ − 1) − a; combined with +1 it forms two's-complement negation.

Category
Bit Manipulation
Time complexity
O(1)
Space complexity
O(1)

Pseudocode

for each bit
  out = 1 − bit
~a = all bits inverted

Reference implementation

~a  # flips every bit
# 8-bit: ~a == 255 - a
# -a == ~a + 1  (two's complement)

Open the interactive Bitwise NOT visualisation →