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

# Dart Bitwise Complement

The `~` operator in Dart is the unary bitwise NOT operator, also known as the bitwise complement operator. It evaluates its operand by inverting every bit in its binary representation, flipping all `0`s to `1`s and all `1`s to `0`s. Because Dart represents signed integers using two's complement, applying the bitwise NOT operator to any integer `n` mathematically yields `-(n + 1)`.

```dart theme={"dark"}
var result = ~operand;
```

## Bitwise Mechanics

When the `~` operator evaluates an integer, it processes the value at the hardware level based on the platform's integer size (64-bit on the Dart Native VM, 32-bit when compiled to JavaScript).

For example, evaluating `~5`:

1. The integer `5` is represented in binary as `...0000000000000101`.
2. The `~` operator applies a logical negation to each individual bit.
3. The resulting binary is `...1111111111111010`.
4. In two's complement architecture, this binary sequence represents the decimal value `-6`.

```dart theme={"dark"}
void main() {
  int a = 5;
  int b = ~a; 
  
  print(b); // Outputs: -6
  
  int c = -12;
  int d = ~c;
  
  print(d); // Outputs: 11
}
```

## Type Constraints and Overloading

In the Dart core library, the `~` operator is defined as an instance method on the `int` and `BigInt` classes. Attempting to apply it to a `double` or a generic `num` (unless explicitly cast or statically inferred as `int`) will result in a compile-time error.

However, Dart supports operator overloading, meaning the `~` operator can be implemented on any custom class by defining `operator ~`.

```dart theme={"dark"}
int intResult = ~42;               // Valid
BigInt bigIntResult = ~BigInt.one; // Valid

// double invalid = ~42.0;         // Compile-time error: The operator '~' isn't defined for the type 'double'.

class BitMask {
  final int _mask;
  BitMask(this._mask);

  // Overloading the unary bitwise NOT operator
  BitMask operator ~() => BitMask(~_mask);
}
```

## Operator Precedence

As a unary prefix operator, `~` has a lower precedence level than unary postfix operators (such as `expr++`, `[]`, `.`, `()`, and the null assertion operator `!`). However, it maintains a higher precedence than multiplicative, additive, shift, and binary bitwise operators. It will evaluate before these lower-precedence operators unless explicitly overridden by parentheses.

```dart theme={"dark"}
int result = ~5 + 2; // Evaluates as (~5) + 2, resulting in -6 + 2 = -4
```

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