Skip to main content
The <<= 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

variable <<= shiftAmount;
This compound assignment is functionally equivalent to:
variable = variable << shiftAmount;

Operational Logic

  1. Bitwise Shift: The operator applies a left shift (<<) to the value of variable.
    • 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.
  2. Assignment: The resulting value is stored in variable.

Mathematical Equivalent

For an integer x and a shift amount n, the operation corresponds to arithmetic multiplication by 2 raised to the power of n.
x = x * (2 ^ n)
Note: For the int type, this equivalence holds only if the result remains within the bit-width limits. If overflow occurs, the result is effectively:
x = (x * (2 ^ n)) mod (2 ^ w)
Where 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.
The right-hand operand (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)

  • int Behavior: Operations occur on 64-bit two’s complement integers. Bits shifted beyond the 64th position are discarded.
  • BigInt Behavior: Operations have arbitrary precision. The value expands indefinitely based on available memory.
  • Shift Amount Validation: The shiftAmount must be non-negative. A negative shift amount throws an ArgumentError.

Dart Web (JavaScript)

  • int Behavior: To maintain compatibility with JavaScript bitwise operators, int operations are truncated to 32-bit signed integers.
    • Bits shifted beyond the 32nd position are discarded.
    • Shift Amount Masking: The shiftAmount is 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.
  • BigInt Behavior: Operations retain arbitrary precision (mapped to JavaScript BigInt).
  • Shift Amount Validation (BigInt): Unlike int on the web, BigInt operations require a non-negative shiftAmount. A negative shift throws an error (typically a JavaScript RangeError wrapped by Dart).

Code Example

The following example demonstrates the mutation of int and BigInt variables using the <<= operator.
void main() {
  // Example 1: Standard int
  int value = 3; // Binary: ...0000 0011
  
  value <<= 2;   // Shift left by 2
  // Calculation: 3 * (2^2) = 12
  // Binary:      ...0000 1100
  print(value);  // Output: 12

  // Example 2: BigInt
  BigInt bigValue = BigInt.from(5); // Binary: ...0000 0101
  
  bigValue <<= 4; // Shift left by 4
  // Calculation: 5 * (2^4) = 80
  // Binary:      ...0101 0000
  print(bigValue); // Output: 80
}
Master Dart with Deep Grasping Methodology!Learn More