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

# JavaScript Bitwise NOT

The bitwise NOT operator (`~`) is a unary operator that inverts the bits of its numeric operand. Its exact execution mechanics and return type depend on whether the operand evaluates to a standard JavaScript `Number` or a `BigInt`.

```javascript theme={"dark"}
~operand
```

## Execution Mechanics: Standard `Number`

When the `~` operator is applied to a standard `Number` (or a non-`BigInt` value that coerces to a `Number`), the JavaScript engine performs the following sequence of operations:

1. **Type Coercion:** The operand is coerced into a numeric value.
2. **Integer Conversion (`ToInt32`):** The numeric value is converted to a 32-bit signed integer using Two's complement representation. Fractional components are truncated. Special numeric values (`NaN`, `Infinity`, `-Infinity`) are converted to `0`.
3. **Bitwise Inversion:** Every bit in the 32-bit sequence is flipped, transforming every `0` to `1` and every `1` to `0`.
4. **Return:** The resulting 32-bit integer is returned as a standard JavaScript `Number` (IEEE 754 double-precision float).

## Execution Mechanics: `BigInt`

When the `~` operator is applied to a `BigInt` primitive, the behavior changes to accommodate arbitrary-precision integers:

1. **No Truncation:** The JavaScript engine does not perform a `ToInt32` conversion. The value is not truncated to 32 bits.
2. **Infinite Two's Complement Inversion:** The operator flips the bits of the `BigInt`. Because `BigInt` values do not have a fixed bit-width, the operation conceptually treats the value as a Two's complement representation with an infinite number of leading sign bits.
3. **Return:** The result is returned as a `BigInt`.

## Mathematical Equivalence

Because JavaScript represents signed integers using Two's complement, the bitwise inversion of any integer `x` (whether a `Number` or a `BigInt`) mathematically evaluates to `-(x + 1)`.

```javascript theme={"dark"}
~x === -(x + 1)
```

## Evaluation Examples

**Standard Integers (`Number`):**

```javascript theme={"dark"}
~5;  // Returns -6
~-5; // Returns 4
~0;  // Returns -1
~-0; // Returns -1
```

* `5` in 32-bit binary: `00000000000000000000000000000101`
* Inverted bits: `11111111111111111111111111111010`
* Two's complement decimal value: `-6`

**Arbitrary-Precision Integers (`BigInt`):**

```javascript theme={"dark"}
~1n;  // Returns -2n
~-5n; // Returns 4n
~0n;  // Returns -1n
```

**Floating-Point Truncation:**
Because the operator forces a `ToInt32` conversion on standard numbers, fractional parts are discarded before inversion.

```javascript theme={"dark"}
~5.99; // Returns -6 (5.99 is truncated to 5 before inversion)
```

**Type Coercion:**
Non-numeric types (excluding `BigInt`) are coerced to standard numbers before the `ToInt32` conversion.

```javascript theme={"dark"}
~"5";       // Returns -6 (String "5" coerced to Number 5)
~true;      // Returns -2 (Boolean true coerced to Number 1)
~null;      // Returns -1 (null coerced to Number 0)
~undefined; // Returns -1 (undefined coerced to NaN, then to 0)
~NaN;       // Returns -1 (NaN converted to 0)
```

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