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.
>>= (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 = x >> y, except that the reference to x is evaluated only once.
Technical Mechanics
When the JavaScript engine evaluatesx >>= y, it performs the following sequence of operations:
- Numeric Coercion (
ToNumeric): Both operands are evaluated and coerced to numeric values.- If either operand is a
Symbol, aTypeErroris thrown. - If the resulting coerced types differ (e.g., one evaluates to a
Numberand the other to aBigInt), aTypeErroris thrown.
- If either operand is a
- Type-Specific Shift Execution:
- For
NumberOperands:- 32-bit Conversion: The left operand undergoes
ToInt32(converted to a 32-bit signed integer using Two’s complement). The right operand undergoesToUint32(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 between0and31. Note that this is a strict bitwise mask, which differs from the remainder operator (%) for negative numbers (e.g.,-1 & 0x1Fevaluates to31, whereas-1 % 32evaluates to-1). - Bitwise Shift: The bits of
xare shifted right by the maskedyvalue. Bits shifted past the 0th position (Least Significant Bit) are discarded. - Sign Propagation: The Most Significant Bit (MSB) of the original 32-bit integer
xdictates the bits shifted in from the left. Ifxis positive (MSB is0),0s are shifted in. Ifxis negative (MSB is1),1s are shifted in.
- 32-bit Conversion: The left operand undergoes
- For
BigIntOperands:- 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
RangeErroris thrown. This is a strict departure fromNumberoperands, 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.
- For
- Assignment: The resulting value is assigned back to the variable
x.
Evaluation Examples
Positive Integer Shift (Number)
Negative Integer Shift (Number)
Bitwise Masking Behavior (Number)
BigInt Shift and Rounding
Type Coercion and Errors
Master JavaScript with Deep Grasping Methodology!Learn More





