Skip to main content
The right shift assignment operator (>>=) is a compound assignment operator that performs a bitwise arithmetic right shift on the left operand by the number of bit positions specified by the right operand, and subsequently assigns the result back to the left operand.

Syntax

variable >>= numberOfBits;
This expression is semantically equivalent to:
variable = variable >> numberOfBits;

Operational Mechanics

  1. Type Constraint: Both operands must be of type int.
  2. Bitwise Shift: The binary representation of variable is shifted to the right by numberOfBits.
  3. Sign Extension: Dart uses arithmetic shifting. The empty bit positions created on the left (most significant bits) are filled with the value of the original sign bit:
    • If the number is positive (sign bit 0), 0s are shifted in.
    • If the number is negative (sign bit 1), 1s are shifted in to preserve the sign in two’s complement representation.
  4. Truncation: Bits shifted off the right end (least significant bits) are discarded.
  5. Assignment: The resulting integer value is stored in variable.

Examples

Positive Integer Shift Shifting a positive integer fills the most significant bits with 0.
void main() {
  int x = 12;      // Binary: 0000 ... 1100
  
  x >>= 2;         // Shift right by 2 positions
                   // Binary: 0000 ... 0011
                   
  print(x);        // Output: 3
}
Negative Integer Shift (Sign Extension) Shifting a negative integer fills the most significant bits with 1.
void main() {
  int y = -8;      // Binary (64-bit two's complement): 1111 ... 1000
  
  y >>= 1;         // Shift right by 1 position
                   // Binary: 1111 ... 1100
                   
  print(y);        // Output: -4
}
Master Dart with Deep Grasping Methodology!Learn More