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 evaluates two operands and shifts the binary representation of the first operand to the right by the number of bits specified by the second operand. It preserves the sign of the original value by filling the vacated leftmost bits with a copy of the original sign bit. In TypeScript, this operator supports both number and bigint types, provided both operands share the same type.
operand1 >> operand2

Technical Mechanics

The underlying mechanics of the >> operator diverge significantly depending on whether the operands are of type number or bigint.

When using number operands:

  1. Operand Coercion: TypeScript (via JavaScript runtime semantics) implicitly converts both operands to 32-bit signed integers using the abstract ToInt32 operation. Any fractional parts are truncated.
  2. Shift Masking: The right operand (operand2) is masked to 5 bits (operand2 & 0x1F). Consequently, the shift amount is evaluated modulo 32. Shifting by 32 bits is equivalent to shifting by 0 bits. Because of this bitwise masking, negative shift amounts wrap around; for example, a >> -1 evaluates as a >> 31.
  3. Bitwise Shift & Sign Propagation: The 32-bit binary representation of operand1 is shifted right. The operator inspects the most significant bit (MSB) before the shift:
    • If the MSB is 0 (positive number), vacated left bits are filled with 0s.
    • If the MSB is 1 (negative number in two’s complement), vacated left bits are filled with 1s.
    Bits shifted off the right boundary are permanently discarded.

When using bigint operands:

  1. Arbitrary Precision: Operands operate as arbitrary-precision integers. They are not coerced or truncated to 32-bit boundaries.
  2. Unmasked Shift & Negative Bounds: The right operand is not masked to 5 bits. It dictates the exact number of bits to shift, allowing for shifts far exceeding 32 bits. However, unlike number operands, shifting a bigint by a negative amount (e.g., 10n >> -1n) is invalid and throws a RangeError at runtime.
  3. Sign Propagation: The sign is preserved mathematically. Shifting a negative bigint right rounds down towards negative infinity, conceptually padding the left with an infinite sequence of 1s.

Behavior Visualization

Number Shift:
const a: number = 10;      // Binary: 00000000000000000000000000001010
const resA = a >> 2;       // Binary: 00000000000000000000000000000010
// resA === 2

const b: number = -10;     // Binary: 11111111111111111111111111110110
const resB = b >> 2;       // Binary: 11111111111111111111111111111101
// resB === -3

const c: number = 100;
const resC1 = c >> 33;     // 33 & 31 = 1. Equivalent to c >> 1.
// resC1 === 50

const resC2 = c >> -1;     // -1 & 31 = 31. Equivalent to c >> 31.
// resC2 === 0
BigInt Shift:
const d: bigint = 10n;
const resD = d >> 2n;
// resD === 2n

const e: bigint = -10n;
const resE = e >> 2n;
// resE === -3n

const f: bigint = 10000000000n;
const resF = f >> 33n;     // No modulo 32 masking. Shifts by exactly 33 bits.
// resF === 1n

// const invalidShift = f >> -1n; 
// Throws RangeError: Right operand of shift operator must be positive

Type Signatures

TypeScript’s static type checker requires both operands to be of the exact same type—either both number or both bigint. The operator returns a value matching the type of the operands. Mixing number and bigint will result in a compilation error (TS2365).
function rightShiftNum(a: number, b: number): number {
    return a >> b;
}

function rightShiftBigInt(a: bigint, b: bigint): bigint {
    return a >> b;
}

// Invalid: Mixing types
// const error = 10 >> 2n; // TS2365: Operator '>>' cannot be applied to types 'number' and 'bigint'.
Master TypeScript with Deep Grasping Methodology!Learn More