TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
>> (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.
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:
-
Operand Coercion: TypeScript (via JavaScript runtime semantics) implicitly converts both operands to 32-bit signed integers using the abstract
ToInt32operation. Any fractional parts are truncated. -
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 >> -1evaluates asa >> 31. -
Bitwise Shift & Sign Propagation: The 32-bit binary representation of
operand1is 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 with0s. - If the MSB is
1(negative number in two’s complement), vacated left bits are filled with1s.
- If the MSB is
When using bigint operands:
- Arbitrary Precision: Operands operate as arbitrary-precision integers. They are not coerced or truncated to 32-bit boundaries.
- 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
numberoperands, shifting abigintby a negative amount (e.g.,10n >> -1n) is invalid and throws aRangeErrorat runtime. - Sign Propagation: The sign is preserved mathematically. Shifting a negative
bigintright rounds down towards negative infinity, conceptually padding the left with an infinite sequence of1s.
Behavior Visualization
Number Shift:Type Signatures
TypeScript’s static type checker requires both operands to be of the exact same type—either bothnumber 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).
Master TypeScript with Deep Grasping Methodology!Learn More





