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.
<< (Left Shift) operator is a binary bitwise operator that shifts the binary representation of its left operand to the left by the number of bits specified by its right operand. In TypeScript, the operator’s mechanical behavior depends entirely on whether the operands are of type number or bigint.
Syntax
Execution Mechanics: number Operands
When both operands are of type number, the operator adheres to the following 32-bit integer semantics:
- Type Coercion: Both operands are implicitly converted to 32-bit signed integers (
Int32) using two’s complement representation. Any fractional parts are truncated. - Right Operand Masking (Modulo 32): The shift amount is strictly evaluated as
expression2 & 0x1F(equivalent toexpression2 % 32). Attempting to shift by 32 bits or more wraps around. For example, shifting by 33 is identical to shifting by 1. - Bit Shifting & Zero-Fill: The bits of the left operand are moved to the left. Vacated bit positions on the right are filled with zeroes (
0). - Discard & Sign Inversion: Bits shifted beyond the 32nd bit boundary on the left are permanently discarded. Because the result is constrained to a 32-bit signed integer, shifting a
1into the most significant bit (MSB, the 32nd bit) alters the sign of the resulting number, yielding a negative value. - Return: The operation evaluates to a standard TypeScript
number.
Execution Mechanics: bigint Operands
When both operands are of type bigint, the operator scales to arbitrary-precision integers:
- No 32-bit Truncation: The operation does not coerce values to 32-bit integers. The binary representation grows as needed to accommodate the shifted value.
- No Masking: The right operand is not subjected to a modulo 32 operation.
1n << 33nwill literally shift the bit 33 positions to the left. - No Discarding: Bits are never discarded beyond a boundary.
- Return: The operation evaluates to a
bigint.
bigint, otherwise a runtime RangeError is thrown).
TypeScript Static Type Checking
Unlike JavaScript’s runtime semantics—which implicitly coerce non-numeric types likeundefined or null to 0—TypeScript’s static type checker strictly forbids this.
- Valid Types: Operands must be of type
number,bigint,any, or anenum. - Type Mixing: TypeScript forbids mixing
numberandbigintacross the operator. Attempting1n << 2results inTS2365: Operator '<<' cannot be applied to types 'bigint' and 'number'. - Non-Numeric Operands: Attempting to use types like
undefined,string, orbooleanwill throw a compilation error:TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
Mathematical Equivalence
Mechanically,a << b yields the exact integer result of . For number types, this equivalence only holds true provided the operation does not trigger a 32-bit overflow. For bigint types, this mathematical equivalence is absolute.
Code Visualization
Master TypeScript with Deep Grasping Methodology!Learn More





