Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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

~operand
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.
Operand:  0 1 0 1   1 0 1 0  (Binary)
          | | | |   | | | |
Result:   1 0 1 0   0 1 0 1  (Binary)

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.
#include <stdint.h>

uint8_t a = 0x0F;        // Binary: 0000 1111
                         // Promoted to 32-bit int (zero-extended): 
                         // 00000000 00000000 00000000 00001111

int result = ~a;         // Binary: 11111111 11111111 11111111 11110000
                         // Hex: 0xFFFFFFF0
                         // Type: int (not uint8_t)
To retain the original bit-width, the result must be explicitly cast back to the smaller type:
uint8_t b = (uint8_t)~a; // Binary: 1111 0000 (0xF0)

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:
~x == -x - 1
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.
Master C with Deep Grasping Methodology!Learn More