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

int result = operand >> shiftAmount;
  • operand: The int value 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:
  1. Bit Discard: Bits are shifted out of the rightmost (Least Significant) position and lost.
  2. 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 shiftAmount must be non-negative. If shiftAmount < 0, the runtime throws a RangeError.
  • Large Shifts: If the shiftAmount exceeds 63, the result is 0 (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 shiftAmount is 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, >> -1 behaves 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 the shiftAmount nn 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 2n2^n with flooring (rounding towards negative infinity): result=operand2nresult = \lfloor \frac{operand}{2^n} \rfloor Note: On Dart Web, if n32n \ge 32, 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 is 0, so zeros are filled in from the left.
void main() {
  // Binary (simplified 8-bit view): 00001100 (Decimal 12)
  int value = 12;
  
  // Shift right by 2
  // 1. Discard rightmost bits (00)
  // 2. Fill left with sign bit (0)
  int result = value >> 2; 
  
  // Result Binary: 00000011 (Decimal 3)
  print(result); // Output: 3
}

Negative Integer Shift (Sign Extension)

When shifting a negative integer, the sign bit is 1, so ones are filled in from the left.
void main() {
  // Binary (simplified 8-bit view): 11110100 (Decimal -12)
  int value = -12;
  
  // Shift right by 2
  // 1. Discard rightmost bits (00)
  // 2. Fill left with sign bit (1)
  int result = value >> 2;
  
  // Result Binary: 11111101 (Decimal -3)
  print(result); // Output: -3
}
Master Dart with Deep Grasping Methodology!Learn More