TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
~ (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 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).
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 integerx is equivalent to its arithmetic negation minus one:
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-xportion overflows ifxisINT_MIN. - In the expression
-(x + 1), thex + 1portion overflows ifxisINT_MAX.
~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





