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 >>= (sign-propagating right shift assignment) operator shifts the binary representation of the left operand to the right by the number of bits specified by the right operand, preserves the sign bit, and assigns the evaluated result back to the left operand.

Syntax

x >>= y
This is syntactically equivalent to x = x >> y, except that the reference to x is evaluated only once.

Technical Mechanics

When the JavaScript engine evaluates x >>= y, it performs the following sequence of operations:
  1. Numeric Coercion (ToNumeric): Both operands are evaluated and coerced to numeric values.
    • If either operand is a Symbol, a TypeError is thrown.
    • If the resulting coerced types differ (e.g., one evaluates to a Number and the other to a BigInt), a TypeError is thrown.
  2. Type-Specific Shift Execution:
    • For Number Operands:
      • 32-bit Conversion: The left operand undergoes ToInt32 (converted to a 32-bit signed integer using Two’s complement). The right operand undergoes ToUint32 (converted to a 32-bit unsigned integer). Fractional parts are truncated.
      • Right Operand Masking: The right operand is masked to its lowest 5 bits using a bitwise AND operation (y & 0x1F). This restricts the shift count to a value between 0 and 31. Note that this is a strict bitwise mask, which differs from the remainder operator (%) for negative numbers (e.g., -1 & 0x1F evaluates to 31, whereas -1 % 32 evaluates to -1).
      • Bitwise Shift: The bits of x are shifted right by the masked y value. Bits shifted past the 0th position (Least Significant Bit) are discarded.
      • Sign Propagation: The Most Significant Bit (MSB) of the original 32-bit integer x dictates the bits shifted in from the left. If x is positive (MSB is 0), 0s are shifted in. If x is negative (MSB is 1), 1s are shifted in.
    • For BigInt Operands:
      • Arbitrary Precision: BigInts do not undergo 32-bit truncation, and the right operand is not subjected to 5-bit masking.
      • Negative Shift Validation: If the right operand is negative, a RangeError is thrown. This is a strict departure from Number operands, which safely wrap negative shift counts via bitwise masking.
      • Mathematical Shift: The left operand is shifted right by the exact value of the right operand. Mathematically, this evaluates to the floor of the left operand divided by 2 to the power of the right operand: ⌊x / 2y⌋. This operation rounds towards negative infinity, which is a critical distinction from JavaScript’s BigInt division operator (/) that truncates towards zero.
  3. Assignment: The resulting value is assigned back to the variable x.

Evaluation Examples

Positive Integer Shift (Number)

let a = 10; 
// 32-bit binary: 00000000000000000000000000001010

a >>= 2;    
// Shifts right by 2. MSB is 0, so 0s are pushed from the left.
// Result binary: 00000000000000000000000000000010
// a is now 2

Negative Integer Shift (Number)

let b = -10; 
// 32-bit binary: 11111111111111111111111111110110 (Two's complement)

b >>= 2;     
// Shifts right by 2. MSB is 1, so 1s are pushed from the left.
// Result binary: 11111111111111111111111111111101
// b is now -3

Bitwise Masking Behavior (Number)

let c = 100;
c >>= 33; 
// 33 & 0x1F evaluates to 1. 
// The operation executed is effectively c >>= 1.
// c is now 50

let d = 100;
d >>= -1;
// -1 & 0x1F evaluates to 31.
// The operation executed is effectively d >>= 31.
// d is now 0

BigInt Shift and Rounding

let e = 100n;
e >>= 33n;
// No 5-bit masking occurs. Shifts right by exactly 33 bits.
// e is now 0n

let f = -5n;
f >>= 1n;
// Mathematically: floor(-5 / 2^1) = -3.
// f is now -3n. 
// (Note: -5n / 2n evaluates to -2n, demonstrating the rounding difference).

Type Coercion and Errors

let g = 15.99;
g >>= "1"; 
// "1" is coerced to Number 1. 15.99 undergoes ToInt32 (becomes 15).
// g is now 7

let h = 10n;
h >>= -1n;
// RangeError: Right-hand side of 'shift right' operator must be non-negative

let i = 10n;
i >>= 2; 
// TypeError: Cannot mix BigInt and other types, use explicit conversions

let j = 5;
j >>= Symbol("shift"); 
// TypeError: Cannot convert a Symbol value to a number
Master JavaScript with Deep Grasping Methodology!Learn More