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

The `^` operator in Dart functions as the exclusive OR (XOR) operator. It evaluates two operands and returns a result based on strict inequality between their corresponding values or bits. Dart implements this operator natively for `int`, `bool`, `BigInt` (in `dart:core`), and the SIMD numeric type `Int32x4` (in `dart:typed_data`).

## Operator Precedence

In Dart, the `^` operator follows a strict precedence hierarchy that dictates how expressions are parsed. Notably, Dart's bitwise operator precedence differs from languages like C, C++, and Java regarding equality operators.

* **Higher than Equality:** `^` has higher precedence than `==` and `!=`.
* **Lower than Bitwise AND:** `^` has lower precedence than `&`.
* **Higher than Bitwise OR:** `^` has higher precedence than `|`.

Because `^` binds more tightly than `==`, an expression like `a ^ b == c` evaluates as `(a ^ b) == c`. Failing to account for this departure from C-style precedence is a common source of logic bugs.

**Syntax and Evaluation:**

```dart theme={"dark"}
int a = 5;  // 0101
int b = 3;  // 0011
int c = 6;  // 0110

// Evaluates as: (a ^ b) == c
// 1. a ^ b -> 6
// 2. 6 == 6 -> true
bool isMatch = a ^ b == c; 

// Evaluates as: (a & b) ^ c
// 1. a & b -> 1 (0001)
// 2. 1 ^ c -> 7 (0111)
int precedenceResult1 = a & b ^ c;

// Evaluates as: (a ^ b) | c
// 1. a ^ b -> 6 (0110)
// 2. 6 | c -> 6 (0110)
int precedenceResult2 = a ^ b | c;
```

## Bitwise XOR (`int` and `BigInt`)

When applied to integers, the `^` operator compares the two's complement binary representations of both operands bit by bit. It returns a new integer where each bit is set to `1` if the corresponding bits of the operands differ, and `0` if they are identical.

**Platform-Specific Semantics and `BigInt`:**
The bit-width of the integer representation evaluated by the bitwise XOR operator on the `int` type depends strictly on the compilation target:

* **Dart Native (VM/AOT):** Operates on 64-bit two's complement binary representations.
* **Dart Web (JavaScript):** Restricts bitwise operations on `int` to 32-bit integers. When compiled to the web, Dart truncates `int` operands to 32 bits before applying the XOR operation.

To bypass this web limitation and safely perform 64-bit (or arbitrarily larger) bitwise XOR operations consistently across all platforms, Dart provides the `BigInt` class. `BigInt` handles arbitrary-precision integers and does not suffer from the 32-bit truncation enforced on `int` in web environments.

**Bitwise Truth Table:**

| Bit A | Bit B | A ^ B |
| :---: | :---: | :---: |
|   0   |   0   |   0   |
|   0   |   1   |   1   |
|   1   |   0   |   1   |
|   1   |   1   |   0   |

**Syntax and Evaluation:**

```dart theme={"dark"}
// Standard int XOR
int x = 5;  // Binary representation: ...0101
int y = 3;  // Binary representation: ...0011

int result = x ^ y; 
// Bitwise comparison:
//   0101 (5)
// ^ 0011 (3)
// ---
//   0110 (6)

// BigInt XOR for safe cross-platform large integer operations
BigInt largeA = BigInt.parse('4294967296'); // 2^32
BigInt largeB = BigInt.from(1);
BigInt largeResult = largeA ^ largeB; 
```

## Logical XOR (`bool`)

When applied to boolean values, the `^` operator evaluates to `true` strictly when one operand is `true` and the other is `false`. If both operands share the same boolean state, the expression evaluates to `false`.

**Logical Truth Table:**

| Operand A | Operand B |  A ^ B  |
| :-------: | :-------: | :-----: |
|  `false`  |  `false`  | `false` |
|  `false`  |   `true`  |  `true` |
|   `true`  |  `false`  |  `true` |
|   `true`  |   `true`  | `false` |

**Syntax and Evaluation:**

```dart theme={"dark"}
bool isTrue = true;
bool isFalse = false;

bool result1 = isTrue ^ isFalse; // Evaluates to true
bool result2 = isTrue ^ isTrue;  // Evaluates to false
```

## Bitwise XOR for SIMD Types

For Single Instruction, Multiple Data (SIMD) operations, the `^` operator performs an element-wise bitwise XOR on the packed data structures.

**Syntax and Evaluation:**

```dart theme={"dark"}
import 'dart:typed_data';

Int32x4 v1 = Int32x4(1, 2, 3, 4);
Int32x4 v2 = Int32x4(5, 6, 7, 8);

// Performs bitwise XOR on each of the four 32-bit lanes independently
Int32x4 vResult = v1 ^ v2; 
```

## Compound Assignment (`^=`)

Dart provides the `^=` compound assignment operator, which performs the XOR operation and immediately assigns the resulting value back to the left-hand variable. It maintains the same type constraints, requiring both operands to be of a compatible type (`int`, `bool`, `BigInt`, or `Int32x4`).

**Syntax:**

```dart theme={"dark"}
// Integer compound assignment
int val = 10; // Binary: 1010
val ^= 6;     // Binary: 0110
              // Result: 1100 (Decimal: 12)

// Boolean compound assignment
bool flag = true;
flag ^= true; // Result: false

// BigInt compound assignment
BigInt bigVal = BigInt.from(10);
bigVal ^= BigInt.from(6); // Result: BigInt.from(12)
```

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