Skip to main content
The &= operator is a compound assignment operator in Dart that performs a bitwise or logical AND operation (&) between the left and right operands, then reassigns the resulting value to the left operand.
variable &= expression;
This operation is syntactically and logically equivalent to:
variable = variable & expression;

Type Support and Reassignment

In Dart, the & operator is defined on the int, BigInt, and bool classes. Because instances of these classes are immutable, the &= operator does not modify the existing object in memory. Instead, it evaluates the & operation to produce a new value and reassigns that value to the left-hand variable. Consequently, the left operand must be a mutable variable (i.e., not declared as const or final).

Integer and BigInt Operations

When applied to int or BigInt types, &= functions as a bitwise AND assignment operator. It compares the binary representations of both operands bit by bit. A resulting bit is set to 1 if and only if the corresponding bits in both operands are 1; otherwise, it is set to 0.
int a = 12;  // Binary representation: 1100
int b = 10;  // Binary representation: 1010

a &= b;      // Performs: 1100 & 1010
             // Result:   1000
             
print(a);    // Output: 8

Boolean Operations

When applied to bool types, &= functions as a non-short-circuiting logical AND assignment operator. Unlike the standard short-circuiting logical AND (&&), the & operator strictly evaluates both the left and right operands. It assigns true to the left variable if and only if both operands evaluate to true.
bool x = true;
bool y = false;

x &= y;      // Performs: true & false
             // Result:   false
             
print(x);    // Output: false
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More