Skip to main content
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:
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 ABit BA ^ B
000
011
101
110
Syntax and Evaluation:
// 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 AOperand BA ^ B
falsefalsefalse
falsetruetrue
truefalsetrue
truetruefalse
Syntax and Evaluation:
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:
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:
// 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)
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More