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

# Python Bitwise NOT

The `~` (bitwise NOT) operator is a unary operator that performs logical negation on each bit of an integer's binary representation, flipping `0`s to `1`s and `1`s to `0`s. Because Python integers have arbitrary precision and are evaluated using two's complement arithmetic, applying the `~` operator to an integer `x` mathematically evaluates to `-(x + 1)`.

```python theme={"dark"}
result = ~x
```

## Underlying Mechanics and Two's Complement

Unlike languages with fixed-width integers (e.g., 32-bit or 64-bit), Python integers conceptually possess an infinite number of sign bits. Positive numbers have an infinite sequence of leading `0`s, while negative numbers have an infinite sequence of leading `1`s.

When the `~` operator flips the bits of a positive integer, the infinite leading `0`s become infinite leading `1`s, transforming the result into a negative number according to two's complement representation.

```python theme={"dark"}
x = 5

# Conceptual binary representation (infinite leading zeros):

# ...00000000 00000101

y = ~x

# Bitwise inversion (infinite leading ones):

# ...11111111 11111010

print(y) 

# Output: -6
```

## Mathematical Equivalence

Regardless of the integer provided, the bitwise NOT operation in Python strictly adheres to the following algebraic identity:

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

## Data Type Support

The `~` operator is natively supported by the `int` and `bool` data types. It is not supported by `float`, `str`, or other non-integral built-in types, which will raise a `TypeError`.

Because `bool` is a subclass of `int` in Python (`True` evaluates to `1` and `False` evaluates to `0`), applying the `~` operator to a boolean yields an integer, not a negated boolean.

```python theme={"dark"}
~True   # Evaluates to -2, because -(1 + 1) = -2
~False  # Evaluates to -1, because -(0 + 1) = -1
```

## Object-Oriented Implementation

At the object level, the `~` operator is mapped to the `__invert__()` magic method (dunder method). When the Python interpreter encounters `~x`, it internally invokes `x.__invert__()`.

You can implement this method in custom classes to define specific behavior for the bitwise NOT operator.

```python theme={"dark"}
class CustomInt:
    def __init__(self, value):
        self.value = value

    def __invert__(self):
        # Custom implementation of the ~ operator
        return CustomInt(-(self.value + 1))

obj = CustomInt(10)
inverted_obj = ~obj
```

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