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 Assignment) operator performs a bitwise left shift on its left operand by the number of bits specified by its right operand, and assigns the computed result back to the left operand.
Execution Mechanics
When evaluatingx <<= y, TypeScript (inheriting from JavaScript’s ECMAScript specification) processes the operation based on the underlying types of the operands.
For number Types
- Type Conversion (
ToInt32): Bothxandyare implicitly coerced into 32-bit signed integers using two’s complement representation. Any fractional components are discarded. - Shift Masking: The right operand
yis masked to 5 bits using a bitwise AND operation (y & 0x1F). Consequently, the actual shift amount is strictly between 0 and 31. This is fundamentally different from the remainder operator (%), as negative shift amounts are safely masked to positive values (e.g.,-1 & 0x1Fevaluates to31, whereas-1 % 32evaluates to-1). - Bit Manipulation: The bits of
xare shifted to the left by the evaluated shift amount. - Zero-Filling: Vacated bit positions on the right are filled with
0s. - Truncation: Any bits shifted beyond the 32-bit boundary on the left are permanently discarded.
- Sign Mutation: Because the 32nd bit (Most Significant Bit) dictates the sign in two’s complement, shifting a
1into this position will mutate a positive integer into a negative integer.
For bigint Types
When both operands are of type bigint, the operator behaves differently:
- Arbitrary Precision: The left operand is treated as an integer of arbitrary size. It is not truncated to 32 bits.
- No Shift Masking: The right operand is not masked to 5 bits. You can shift by amounts greater than 31.
- Negative Shift Amounts: Unlike
numbertypes, shifting abigintby a negative amount is an invalid operation. Attempting to do so (e.g.,bigX <<= -1n) will throw a runtimeRangeError(“Right-hand side of ‘shift left’ operation must be non-negative”). - Type Strictness: TypeScript enforces that both operands must be
bigint. Mixingnumberandbigintwith<<=will result in a compiler error (TS2365).
Mathematical Equivalence
Fornumber types, assuming no 32-bit overflow occurs, x <<= y is mathematically equivalent to:
x = x * (2 ** y)
Master TypeScript with Deep Grasping Methodology!Learn More





