<<) shifts the binary representation of an integer to the left by a specified number of bit positions. It inserts zero bits into the least significant bit (LSB) positions and discards bits shifted beyond the integer’s fixed width.
Syntax
operand: Theintvalue to be shifted.shift_amount: Anintspecifying the number of positions to shift.- Return Value: An
intcontaining the shifted value.
Operational Mechanics
- Bit Displacement: Moves every bit in the
operandto the left byshift_amount. - Zero Padding: Fills the vacated positions on the right (LSB) with
0. - Arithmetic Equivalent: Mathematically equivalent to multiplying the operand by 2 raised to the power of
shift_amount(operand * 2^shift_amount), provided the result does not overflow the platform’s integer limit.
Platform Specifics and Constraints
Dart handles integer width and shift behavior differently depending on the compilation target (Native vs. Web).Dart Native (VM/AOT)
- Width: Operations are performed on fixed 64-bit two’s complement integers.
- Shift Count Handling: The shift amount is not masked. Shifting by 64 or more positions shifts all bits out, resulting in
0. - Exceptions: Throws an
ArgumentErrorifshift_amountis negative. - Overflow: Bits shifted beyond the 64th position are discarded.
Dart Web
- Width: Operations map to the JavaScript bitwise left shift. The
operandis converted to a signed 32-bit integer before the shift occurs. - Shift Count Handling: The effective shift amount is masked to the lower 5 bits (
shift_amount & 0x1F). This results in a cyclic shift range of 0 to 31. For example:x << 32is equivalent tox << 0.x << -1is equivalent tox << 31(because -1 in two’s complement has all lower 5 bits set).
- Exceptions: Does not throw an error for negative shift amounts.
- Overflow: The result is always truncated to a signed 32-bit integer.
Example
The following example demonstrates shifting the integer3 left by 2 positions.
Master Dart with Deep Grasping Methodology!Learn More





