^ 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|.
^ 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:
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
intto 32-bit integers. When compiled to the web, Dart truncatesintoperands to 32 bits before applying the XOR operation.
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 |
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 |
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:
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:
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





