Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The >> operator is the bitwise arithmetic right shift operator in Dart. It shifts the binary representation of the left operand to the right by the number of positions specified by the right operand. Because it is an arithmetic shift, it performs sign extension: the vacant bits on the left are filled with the value of the original sign bit (0 for positive numbers, 1 for negative numbers).
int result = operand >> shiftCount;

Mechanics

  1. Rightward Shift: Each bit in the binary representation of the operand is moved to the right by shiftCount positions.
  2. Discarding Bits: The rightmost bits that are shifted past the least significant bit (LSB) are permanently discarded.
  3. Sign Extension: The leftmost bits (most significant bits, or MSB) vacated by the shift are populated with the original sign bit to maintain the integer’s mathematical sign.

Code Visualization

Positive Integer Shift When shifting a positive integer, the sign bit is 0. Therefore, the vacated leftmost bits are filled with 0.
int a = 16;      // Binary: ...0001 0000
int b = a >> 2;  // Binary: ...0000 0100
print(b);        // Output: 4
Negative Integer Shift Dart represents negative integers using two’s complement. The sign bit for a negative number is 1. Therefore, the vacated leftmost bits are filled with 1.
int x = -16;     // Binary: ...1111 0000 (two's complement)
int y = x >> 2;  // Binary: ...1111 1100
print(y);        // Output: -4

Mathematical Equivalence

Evaluating a >> b is mathematically equivalent to performing standard division (/) of a by 2 raised to the power of b, followed by a .floor() operation. Because .floor() truncates towards negative infinity, the >> operator yields different results for negative numbers compared to Dart’s integer division operator (~/), which truncates towards zero.
import 'dart:math' as math;

void main() {
  // 15 >> 2 is equivalent to (15 / math.pow(2, 2)).floor()
  print(15 >> 2); // Output: 3

  // -15 >> 2 is equivalent to (-15 / math.pow(2, 2)).floor()
  print(-15 >> 2); // Output: -4
  
  // Contrast with integer division (~/), which truncates towards zero:
  print(-15 ~/ 4); // Output: -3
}

Platform-Specific Behavior

When using bitwise operators in Dart, the underlying architecture dictates the bit-width of the operands:
  • Native Platforms (AOT/JIT): Bitwise operations are evaluated using 64-bit signed integers.
  • Web Platforms (JavaScript): Because Dart integers are mapped to JavaScript numbers on the web, operands are truncated to 32-bit signed integers before the bitwise shift is applied. This can result in different outputs across platforms when shifting large integers.
Note: If you require a logical right shift (where vacated bits are strictly filled with 0 regardless of the sign bit), Dart provides the >>> (unsigned right shift) operator.
Master Dart with Deep Grasping Methodology!Learn More