Skip to main content
The >>= (right shift assignment) operator is a compound assignment operator that performs a bitwise right shift on the left operand by the number of bit positions specified by the right operand, and subsequently assigns the resulting value back to the left operand.
This operation is semantically equivalent to:

Technical Mechanics

Operand Constraints:
  • Both the left and right operands must resolve to integer types.
  • The right operand (shift count) dictates the number of bits to shift. If the shift count evaluates to a negative value at runtime, Go will trigger a panic.
Shift Behavior: The underlying bitwise operation depends entirely on the signedness of the left operand’s type:
  1. Logical Right Shift (Unsigned Integers): If the left operand is an unsigned integer type (e.g., uint8, uint64), >>= performs a logical right shift. The bits are shifted to the right, and the vacated Most Significant Bits (MSBs) on the left are filled with zeros.
var x uint8 = 20 // Binary: 0001 0100 x >>= 2 // Shift right by 2 positions // x is now 5 // Binary: 0000 0101 (Zero-filled MSBs)

Out-of-Bounds Shifts

If the right operand specifies a shift count that is equal to or greater than the bit-width of the left operand’s type:
  • For unsigned integers, the result evaluates to 0.
  • For signed integers, the result evaluates to 0 if the original value was positive, or -1 (all bits set to 1) if the original value was negative.
Tired of Poor Go Skills? Fix That With Deep Grasping!Learn More