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 <<= (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.
x <<= y;
// Equivalent to: x = x << y;

Execution Mechanics

When evaluating x <<= y, TypeScript (inheriting from JavaScript’s ECMAScript specification) processes the operation based on the underlying types of the operands.

For number Types

  1. Type Conversion (ToInt32): Both x and y are implicitly coerced into 32-bit signed integers using two’s complement representation. Any fractional components are discarded.
  2. Shift Masking: The right operand y is 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 & 0x1F evaluates to 31, whereas -1 % 32 evaluates to -1).
  3. Bit Manipulation: The bits of x are shifted to the left by the evaluated shift amount.
  4. Zero-Filling: Vacated bit positions on the right are filled with 0s.
  5. Truncation: Any bits shifted beyond the 32-bit boundary on the left are permanently discarded.
  6. Sign Mutation: Because the 32nd bit (Most Significant Bit) dictates the sign in two’s complement, shifting a 1 into this position will mutate a positive integer into a negative integer.
let a: number = 5;      // Binary: 00000000 00000000 00000000 00000101
a <<= 2;                // Binary: 00000000 00000000 00000000 00010100
console.log(a);         // Output: 20

let b: number = 1;      // Binary: 00000000 00000000 00000000 00000001
b <<= 31;               // Binary: 10000000 00000000 00000000 00000000 (MSB is 1)
console.log(b);         // Output: -2147483648

let c: number = 10;
c <<= 33;               // 33 & 0x1F = 1. Equivalent to c <<= 1.
console.log(c);         // Output: 20

let d: number = 10;     // Binary: ...00001010
d <<= -1;               // -1 & 0x1F = 31. Equivalent to d <<= 31.
                        // The '1' bits overflow past the 32-bit boundary and are truncated.
                        // The 0th bit ('0') shifts into the 31st position.
console.log(d);         // Output: 0

For bigint Types

When both operands are of type bigint, the operator behaves differently:
  1. Arbitrary Precision: The left operand is treated as an integer of arbitrary size. It is not truncated to 32 bits.
  2. No Shift Masking: The right operand is not masked to 5 bits. You can shift by amounts greater than 31.
  3. Negative Shift Amounts: Unlike number types, shifting a bigint by a negative amount is an invalid operation. Attempting to do so (e.g., bigX <<= -1n) will throw a runtime RangeError (“Right-hand side of ‘shift left’ operation must be non-negative”).
  4. Type Strictness: TypeScript enforces that both operands must be bigint. Mixing number and bigint with <<= will result in a compiler error (TS2365).
let bigX: bigint = 5n;
bigX <<= 32n;           // Shifts by exactly 32 bits, no masking applied
console.log(bigX);      // Output: 21474836480n

// TypeScript Error: Operator '<<=' cannot be applied to types 'bigint' and 'number'.
// bigX <<= 2; 

let bigY: bigint = 5n;
// bigY <<= -1n;        // Throws runtime RangeError: Right-hand side of 'shift left' operation must be non-negative

Mathematical Equivalence

For number 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