Skip to main content
The ~ (bitwise NOT) operator is a unary operator in C that performs a bitwise negation (or one’s complement) on each individual bit of its integer operand. It evaluates the binary representation of the value, converting every 0 bit to 1 and every 1 bit to 0.

Syntax

The operand must be of an integral type (e.g., char, short, int, long, or their unsigned variants).

Bit-Level Mechanics

When the operator is applied, the evaluation occurs strictly at the binary level.

Integer Promotion

A critical characteristic of the ~ operator in C is that it applies integer promotions to its operand before performing the bitwise inversion. The type of the result is the type of the promoted operand. If you apply ~ to a type smaller than an int (such as uint8_t, int8_t, char, or short), the compiler first widens the value to a standard int (typically 32 bits) or unsigned int. During this promotion, the padding of the new high-order bits depends on the signedness and value of the original type:
  • Unsigned types and non-negative signed values are zero-extended (padded with zeros).
  • Negative signed values undergo sign-extension (padded with ones).
The bitwise inversion then flips all bits of the newly promoted width.
To retain the original bit-width, the result must be explicitly cast back to the smaller type:

Two’s Complement Equivalence and Overflow

Because modern C implementations utilize two’s complement representation for signed integers, the bitwise NOT operation has a direct mathematical relationship with arithmetic negation. Mathematically, the one’s complement of an integer x is equivalent to its arithmetic negation minus one:
For example, if x = 5 (binary 0000 0101), ~x results in -6 (binary 1111 1010 in an 8-bit two’s complement system). Important Caveat Regarding Undefined Behavior: While the mathematical equivalence holds true, expressing this relationship directly as arithmetic C code (e.g., -x - 1 or -(x + 1)) can invoke undefined behavior due to signed integer overflow.
  • In the expression -x - 1, the -x portion overflows if x is INT_MIN.
  • In the expression -(x + 1), the x + 1 portion overflows if x is INT_MAX.
In contrast, the bitwise operation ~x operates solely on the bit representation. It is always well-defined for any valid integer value and never triggers arithmetic overflow.
Tired of Poor C Skills? Fix That With Deep Grasping!Learn More