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

The `~` operator in C++ is the unary bitwise NOT operator, also known as the one's complement operator. It evaluates its operand and returns a value where every bit of the operand's binary representation has been inverted: every `0` becomes a `1`, and every `1` becomes a `0`.

## Syntax

```cpp theme={"dark"}
~expression
```

## Operand and Type Requirements

The operand must be of an integral type or an unscoped enumeration type. Before the bitwise inversion occurs, C++ applies **integral promotions** to the operand:

* Types smaller than `int` (e.g., `char`, `signed char`, `unsigned char`, `short`, `unsigned short`) are promoted to `int` (or `unsigned int` if `int` cannot represent all values of the original type).
* The bitwise inversion is performed on the promoted type.
* The result is a prvalue (pure rvalue) of the promoted type.

## Bitwise Mechanics and Integral Promotion

Because of integral promotion, applying `~` to smaller integer types alters the bit width of the evaluated expression.

```cpp theme={"dark"}
#include <cstdint>
#include <type_traits>

uint8_t val = 0b00001111; // Decimal 15

// 'val' is promoted to a 32-bit int before inversion:
// 00000000 00000000 00000000 00001111
// After ~ operation:
// 11111111 11111111 11111111 11110000
auto result = ~val; 

static_assert(std::is_same_v<decltype(result), int>); // True
```

## Mathematical Equivalence

In C++20 and later, signed integers are strictly defined to use two's complement representation. Under two's complement arithmetic, the bitwise NOT operation is mathematically equivalent to negating the value and subtracting one:

```cpp theme={"dark"}
int x = 5;
int y = ~x; // y is -6
// ~x == -x - 1
```

## Operator Overloading

The `~` operator can be overloaded for user-defined types. When overloaded as a non-member function, it takes exactly one parameter. When overloaded as a member function, it takes no parameters.

```cpp theme={"dark"}
struct CustomType {
    int value;
    
    // Member function overload
    CustomType operator~() const {
        return CustomType{~value};
    }
};
```

## Destructor Token

Distinct from its role as an expression operator, the `~` token is also used syntactically in class definitions to declare a destructor. In this context, it acts as a declarative identifier prepended to the class name and does not perform a bitwise operation.

```cpp theme={"dark"}
class Resource {
public:
    Resource();  // Constructor
    ~Resource(); // Destructor
};
```

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