> ## 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.

# TypeScript Left Shift Assignment

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.

```typescript theme={"dark"}
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 `0`s.
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.

```typescript theme={"dark"}
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`).

```typescript theme={"dark"}
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)`

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
