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 operator (>>) shifts the binary representation of the first operand to the right by the number of bits specified by the second operand. It discards bits shifted off to the right and preserves the original number’s sign by copying the leftmost bit (the sign bit) into the newly vacated bit positions on the left. The operator supports both Number and BigInt types.
a >> b

Technical Mechanics: Number Operands

When both operands are standard JavaScript Number values, the operator applies 32-bit integer semantics:
  1. Type Coercion: The left operand (a) is converted to a 32-bit signed integer using the ECMAScript ToInt32 abstract operation. The right operand (b) is converted to a 32-bit unsigned integer using the ToUint32 abstract operation. Fractional parts are truncated. Non-numeric values (like NaN, null, or undefined) are coerced to 0.
  2. Modulo Shift Amount: The evaluated right operand is masked to 5 bits (b & 0x1F). The effective shift amount is always b % 32. Shifting by 32, 64, or 96 bits is functionally identical to shifting by 0 bits.
  3. Sign Extension: JavaScript uses the 32nd bit of the ToInt32 representation as the sign indicator (0 for positive, 1 for negative). If the original number is positive, 0s are shifted in from the left. If negative, 1s are shifted in from the left, maintaining the two’s complement structure.

Technical Mechanics: BigInt Operands

When both operands are BigInt values, the operator applies arbitrary-precision semantics:
  1. Arbitrary Precision: BigInt values are not truncated to 32 bits. They conceptually operate on an infinite-length two’s complement binary representation.
  2. Unmasked Shift Amount: The right operand is not masked. A BigInt can be shifted by more than 32 bits, and the operation mathematically evaluates to a / (2n ** b) (rounded towards negative infinity).
  3. Sign Extension: The sign is preserved mathematically without relying on a fixed 32nd bit.

Syntax Visualization

Number: Positive Integer Shift When shifting a positive integer, the sign bit is 0, so 0s are populated from the left.
9 >> 2; // Evaluates to 2

// 32-bit binary representation of 9:
// 00000000000000000000000000001001

// Shifted right by 2 positions (vacated bits filled with 0):
// 00000000000000000000000000000010 // Evaluates to 2
Number: Negative Integer Shift When shifting a negative integer, the sign bit is 1, so 1s are populated from the left.
-9 >> 2; // Evaluates to -3

// 32-bit two's complement representation of -9:
// 11111111111111111111111111110111

// Shifted right by 2 positions (vacated bits filled with 1):
// 11111111111111111111111111111101 // Evaluates to -3
BigInt: Arbitrary Precision Shift BigInt shifts process the full magnitude without 32-bit truncation or modulo masking.
10000000000000n >> 33n; // Evaluates to 1164n

// Mathematically equivalent to:
// Math.floor(10000000000000 / (2 ** 33))

Edge Cases and Coercion Behavior

Type Mixing (TypeError) Mixing Number and BigInt operands is strictly prohibited and throws a TypeError.
10n >> 2; // TypeError: Cannot mix BigInt and other types
10 >> 2n; // TypeError: Cannot mix BigInt and other types
Floating-Point Truncation Because of the mandatory ToInt32 conversion for standard numbers, the >> operator strips floating-point decimals before shifting:
10.99 >> 1;   // Evaluates to 5 (10.99 is truncated to 10)
-10.99 >> 1;  // Evaluates to -5 (-10.99 is truncated to -10)
Modulo 32 Behavior (Numbers Only) For standard numbers, shift amounts exceeding 31 are wrapped via the 5-bit mask:
5 >> 32;      // Evaluates to 5 (32 % 32 = 0, so 5 >> 0)
5 >> 33;      // Evaluates to 2 (33 % 32 = 1, so 5 >> 1)
Non-Numeric Coercion Standard type coercion applies to non-numeric Number operands:
NaN >> 1;     // Evaluates to 0 (NaN coerces to 0)
"12" >> 1;    // Evaluates to 6 (String "12" coerces to integer 12)
Master JavaScript with Deep Grasping Methodology!Learn More