Skip to main content
The ^= operator is the bitwise XOR assignment operator. It performs a bitwise exclusive OR (XOR) operation on the variable’s current value and the right-hand operand, then assigns the resulting integer back to the variable.

Syntax

variable ^= expression;
This compound assignment is syntactically equivalent to:
variable = variable ^ expression;

Operational Logic

The operator functions at the binary level, comparing the corresponding bits of the two integer operands:
  1. 1 if the bits differ (one is 0, the other is 1).
  2. 0 if the bits are identical (both 0 or both 1).

Type Constraints

  • Type: Both the variable and the expression must resolve to type int.
  • Nullability: The variable must be non-null.

Example

void main() {
  int value = 5;      // Binary: 0101
  int mask = 3;       // Binary: 0011

  // Operation: 0101 XOR 0011
  // Result:    0110 (Decimal 6)
  value ^= mask;

  print(value); // Output: 6
}

Truth Table (Per Bit)

Bit ABit BResult (A ^ B)
000
011
101
110
Master Dart with Deep Grasping Methodology!Learn More