&= operator is a compound assignment operator that performs a bitwise or logical AND operation on the left-hand operand (variable) and the right-hand operand (expression), assigning the result back to the left-hand operand.
Syntax and Semantics
The expressiona &= b is syntactically equivalent to a = a & b, with the specific semantic distinction that the left-hand operand a is evaluated exactly once.
If the left-hand operand is an expression with side effects (such as a list access with an incrementing index), those side effects occur only once during the operation.
Supported Types
The operator functions on any type that defines the& operator. In the Dart core libraries, this includes:
intBigIntbool
Bitwise AND (Integers)
When applied toint or BigInt types, &= performs a bitwise AND operation on the binary representation of the operands. The resulting bit is set to 1 only if the corresponding bits of both operands are 1.
Logical AND (Booleans)
When applied tobool types, &= performs an eager logical AND. Unlike the short-circuit assignment operator (&&=), the &= operator always evaluates the right-hand operand because the underlying & operator on booleans is non-short-circuiting.
- If both operands are
true, the variable is assignedtrue. - If either operand is
false, the variable is assignedfalse.
Operator Overloading Requirements
The&= operator can be used with custom types if the class of the left-hand operand defines operator &. The usage is subject to the following type constraints:
- Parameter Type: The right-hand operand must be of a type accepted by the defined
operator &. - Return Type: The return type of the
operator &must be assignable to the type of the left-hand variable.
Master Dart with Deep Grasping Methodology!Learn More





