|= operator is the bitwise OR assignment operator. It performs a bitwise inclusive OR operation on the variable on the left-hand side (LHS) and the expression on the right-hand side (RHS), then assigns the resulting value back to the LHS variable.
Syntax
Semantics
The compound assignment expressiona |= b is semantically equivalent to:
a |= b, the LHS operand a is evaluated only once.
Bitwise Logic
When applied to integer types, the operator processes operands at the binary level. It compares corresponding bits of both operands:- If either bit is
1, the resulting bit is1. - If both bits are
0, the resulting bit is0.
| Bit A | Bit B | Result (A | B) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Supported Types
The|= operator is applicable to any type that defines the bitwise OR operator (|).
- Core Types: Dart supports this operator natively for
intandBigInt. - Custom Types: User-defined classes can support
|=by overloading the|operator. - Type Safety: The return type of the
|operation must be assignable to the type of the LHS variable.
Example: Integer Mutation
The following example demonstrates the mutation of anint variable using the |= operator.
Operator Overloading
Dart does not allow explicit overloading of the|= operator itself. Instead, a class must overload the standard bitwise OR operator (|). If a class defines operator |, Dart automatically enables the |= syntax for that class.
Master Dart with Deep Grasping Methodology!Learn More





