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 >>= (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.
x >>= y
This operation is semantically equivalent to:
x = x >> y

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)
2. **Arithmetic Right Shift (Signed Integers):**
   If the left operand is a signed integer type (e.g., `int8`, `int64`), `>>=` performs an arithmetic right shift. The bits are shifted to the right, but the vacated MSBs are filled with copies of the original sign bit (0 for positive numbers, 1 for negative numbers). This preserves the sign of the two's complement integer.
   ```go
var y int8 = -20 // Binary: 1110 1100 (Two's complement)
y >>= 2          // Shift right by 2 positions
// y is now -5   // Binary: 1111 1011 (Sign-bit 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.
Master Go with Deep Grasping Methodology!Learn More