> ## 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.

# C Bitwise Complement

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

```c theme={"dark"}
~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.

```text theme={"dark"}
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.

```c theme={"dark"}
#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:

```c theme={"dark"}
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:

```text theme={"dark"}
~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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
