> ## 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 Right Shift

The `>>` (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.

```typescript theme={"dark"}
operand1 >> operand2
```

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

1. **Operand Coercion:** TypeScript (via JavaScript runtime semantics) implicitly converts both operands to 32-bit signed integers using the abstract `ToInt32` operation. Any fractional parts are truncated.
2. **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 >> -1` evaluates as `a >> 31`.
3. **Bitwise Shift & Sign Propagation:** The 32-bit binary representation of `operand1` is 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 with `0`s.
   * If the MSB is `1` (negative number in two's complement), vacated left bits are filled with `1`s.

   Bits shifted off the right boundary are permanently discarded.

### When using `bigint` operands:

1. **Arbitrary Precision:** Operands operate as arbitrary-precision integers. They are *not* coerced or truncated to 32-bit boundaries.
2. **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 `number` operands, shifting a `bigint` by a negative amount (e.g., `10n >> -1n`) is invalid and throws a `RangeError` at runtime.
3. **Sign Propagation:** The sign is preserved mathematically. Shifting a negative `bigint` right rounds down towards negative infinity, conceptually padding the left with an infinite sequence of `1`s.

## Behavior Visualization

**Number Shift:**

```typescript theme={"dark"}
const a: number = 10;      // Binary: 00000000000000000000000000001010
const resA = a >> 2;       // Binary: 00000000000000000000000000000010
// resA === 2

const b: number = -10;     // Binary: 11111111111111111111111111110110
const resB = b >> 2;       // Binary: 11111111111111111111111111111101
// resB === -3

const c: number = 100;
const resC1 = c >> 33;     // 33 & 31 = 1. Equivalent to c >> 1.
// resC1 === 50

const resC2 = c >> -1;     // -1 & 31 = 31. Equivalent to c >> 31.
// resC2 === 0
```

**BigInt Shift:**

```typescript theme={"dark"}
const d: bigint = 10n;
const resD = d >> 2n;
// resD === 2n

const e: bigint = -10n;
const resE = e >> 2n;
// resE === -3n

const f: bigint = 10000000000n;
const resF = f >> 33n;     // No modulo 32 masking. Shifts by exactly 33 bits.
// resF === 1n

// const invalidShift = f >> -1n; 
// Throws RangeError: Right operand of shift operator must be positive
```

## Type Signatures

TypeScript's static type checker requires both operands to be of the exact same type—either both `number` 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`).

```typescript theme={"dark"}
function rightShiftNum(a: number, b: number): number {
    return a >> b;
}

function rightShiftBigInt(a: bigint, b: bigint): bigint {
    return a >> b;
}

// Invalid: Mixing types
// const error = 10 >> 2n; // TS2365: Operator '>>' cannot be applied to types 'number' and 'bigint'.
```

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