>> operator performs an arithmetic (sign-extending) bitwise right shift on an integer. It shifts the bits of the left operand to the right by the distance specified by the right operand, discarding the least significant bits and filling the most significant bits with the sign bit of the original value.
Syntax
- operand: The
intvalue to be shifted. - shiftAmount: The number of bit positions to shift.
Operational Mechanics
The>> operator preserves the sign of the number (Two’s Complement representation) during the shift:
- Bit Discard: Bits are shifted out of the rightmost (Least Significant) position and lost.
- Sign Extension: The empty positions created on the left (Most Significant) are filled with the value of the original sign bit:
- 0 if the operand is positive or zero.
- 1 if the operand is negative.
Platform Specifics
Dart behaves differently depending on whether the code is compiled to native machine code (mobile/desktop/server) or JavaScript (web).Dart Native (VM/AOT)
- Bit Width: Operations are performed on 64-bit integers.
- Shift Amount Validation: The
shiftAmountmust be non-negative. IfshiftAmount < 0, the runtime throws aRangeError. - Large Shifts: If the
shiftAmountexceeds 63, the result is0(for positive numbers) or-1(for negative numbers), adhering to the mathematical logic of shifting out all significant bits.
Dart Web (JavaScript)
- Bit Width: Operations are performed on 32-bit integers. The operand is truncated to 32 bits before shifting.
- Shift Amount Masking: The
shiftAmountis masked to the lower 5 bits (shiftAmount & 0x1F). This results in an effective shift range of 0 to 31. - No RangeError: Negative shift amounts do not throw an error. Instead, they are treated as
shiftAmount & 0x1F. For example,>> -1behaves as>> 31. - Large Shifts: Because of the 5-bit mask, shifting by 32 results in a shift of 0 (
32 & 0x1F == 0).
Mathematical Equivalent
Provided theshiftAmount is non-negative and less than the platform’s bit-width (64 for Native, 32 for Web), the operation is equivalent to integer division by with flooring (rounding towards negative infinity):
Note: On Dart Web, if , this formula does not hold due to the shift count masking described above.
Examples
Positive Integer Shift
When shifting a positive integer, the sign bit is0, so zeros are filled in from the left.
Negative Integer Shift (Sign Extension)
When shifting a negative integer, the sign bit is1, so ones are filled in from the left.
Master Dart with Deep Grasping Methodology!Learn More





