<<= operator is the bitwise left shift assignment operator. It shifts the bits of the left operand to the left by the number of positions specified by the right operand, truncating or expanding the storage based on the type, and assigns the result back to the left operand.
Syntax
Operational Logic
- Bitwise Shift: The operator applies a left shift (
<<) to the value ofvariable.- Bits are shifted to the left by
shiftAmount. - Zero bits (
0) are inserted into the vacated rightmost positions (least significant bits). - Storage Behavior:
int: The operation behaves as if acting on a fixed-width integer. Bits shifted beyond the platform-specific width are discarded.BigInt: The storage expands to accommodate the new bits; no high-order bits are discarded.
- Bits are shifted to the left by
- Assignment: The resulting value is stored in
variable.
Mathematical Equivalent
For an integerx and a shift amount n, the operation corresponds to arithmetic multiplication by 2 raised to the power of n.
int type, this equivalence holds only if the result remains within the bit-width limits. If overflow occurs, the result is effectively:
w is the bit-width of the platform implementation (64 or 32).
Supported Types
The operator is defined for the following numeric types:int: Platform-dependent integers.BigInt: Arbitrary-precision integers.
shiftAmount) must always be of type int.
Platform-Specific Behavior
The behavior of the shift operation varies based on the compilation target.Dart Native (VM / AOT)
intBehavior: Operations occur on 64-bit two’s complement integers. Bits shifted beyond the 64th position are discarded.BigIntBehavior: Operations have arbitrary precision. The value expands indefinitely based on available memory.- Shift Amount Validation: The
shiftAmountmust be non-negative. A negative shift amount throws anArgumentError.
Dart Web (JavaScript)
intBehavior: To maintain compatibility with JavaScript bitwise operators,intoperations are truncated to 32-bit signed integers.- Bits shifted beyond the 32nd position are discarded.
- Shift Amount Masking: The
shiftAmountis masked to the lower 5 bits (shiftAmount & 0x1F). Consequently, negative shift amounts do not throw an error; they are interpreted based on their masked 5-bit value.
BigIntBehavior: Operations retain arbitrary precision (mapped to JavaScriptBigInt).- Shift Amount Validation (
BigInt): Unlikeinton the web,BigIntoperations require a non-negativeshiftAmount. A negative shift throws an error (typically a JavaScriptRangeErrorwrapped by Dart).
Code Example
The following example demonstrates the mutation ofint and BigInt variables using the <<= operator.
Master Dart with Deep Grasping Methodology!Learn More





