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 >>= (sign-propagating right shift assignment) operator evaluates the binary representation of its left operand, shifts it to the right by the number of bits specified by the right operand, and assigns the result back to the left operand while preserving the original sign. Syntax
x >>= y;
This is the compound assignment equivalent of the standard right shift operator:
x = x >> y;
Underlying Mechanics: number Operands
  1. Operand Conversion: TypeScript (following ECMAScript specifications) implicitly coerces the left operand to a 32-bit signed integer (ToInt32) using two’s complement representation. The right operand is coerced to a 32-bit unsigned integer (ToUint32). Any fractional components are truncated before the shift occurs.
  2. Shift Masking: The right operand is masked with 0x1F (bitwise AND with 31, i.e., y & 0x1F). This ensures the shift amount is always strictly between 0 and 31, preventing out-of-bounds shifts. This bitwise mask is distinct from the remainder operator (%); for example, a negative shift amount like -1 correctly masks to 31 (-1 & 0x1F), whereas -1 % 32 would evaluate to -1.
  3. Sign Propagation: As bits are shifted to the right, the vacated most significant bits (MSB) on the left are filled with a copy of the original sign bit. If the original number was positive, the new bits are 0. If the original number was negative, the new bits are 1.
  4. Bit Discard: Bits shifted past the least significant bit (LSB) on the right are permanently discarded.
Underlying Mechanics: bigint Operands When both operands are of type bigint, the operator behaves differently:
  1. No 32-bit Truncation: Operands are not coerced to 32-bit integers. They are treated as having an infinite-length two’s complement representation.
  2. No Shift Masking: The right operand is not masked with 0x1F. The left operand is shifted by the exact magnitude of the right operand.
  3. Errors: A negative right operand throws a RangeError. Mixing number and bigint operands throws a TypeError.
Behavioral Examples Positive Integer Shift (number):
let a = 20; // Binary: 00000000000000000000000000010100
a >>= 2;    // Shifts right by 2 bits. Vacated MSBs filled with 0.
// Result: 5   (Binary: 00000000000000000000000000000101)
Negative Integer Shift (number):
let b = -20; // Binary: 11111111111111111111111111101100 (Two's complement)
b >>= 2;     // Shifts right by 2 bits. Vacated MSBs filled with 1.
// Result: -5  (Binary: 11111111111111111111111111111011)
Shift Masking (number):
let c = 10;
c >>= 33; // 33 & 0x1F evaluates to 1. Shifts right by 1 bit.
// Result: 5

let d = 10;
d >>= -1; // -1 & 0x1F evaluates to 31. Shifts right by 31 bits.
// Result: 0
BigInt Shift (bigint):
let e = 10n;
e >>= 33n; // No masking occurs. Shifts right by 33 bits.
// Result: 0n

let f = -20n;
f >>= 2n; // Sign propagation still applies to BigInts.
// Result: -5n
Master TypeScript with Deep Grasping Methodology!Learn More