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 >>>= (unsigned 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, fills the vacated most significant bits with zeroes, and assigns the resulting 32-bit unsigned integer back to the left operand.

Syntax

x >>>= y
This is the compound assignment equivalent of the logical right shift operation:
x = x >>> y

Technical Mechanics

When the JavaScript engine evaluates x >>>= y, it performs the following sequence of operations:
  1. Numeric Evaluation (ToNumeric): Both operands are evaluated to numeric values. If the resulting type of either operand is a BigInt, the engine immediately throws a TypeError. The >>> and >>>= operators are the only bitwise operators in JavaScript that explicitly do not support BigInt.
  2. Operand Coercion:
    • The evaluated left operand (x) is coerced into a 32-bit unsigned integer (ToUint32).
    • The evaluated right operand (y) is coerced into a 32-bit unsigned integer (ToUint32).
  3. Shift Count Calculation: The shift amount is determined by applying a mathematical modulo 32 to the coerced right operand (ℝ(y) modulo 32). Because the right operand has already been coerced to a 32-bit unsigned integer, it is strictly non-negative. This operation ensures the resulting shift count is always an integer between 0 and 31 inclusive (which is functionally equivalent to a bitwise AND mask of y & 0x1F).
  4. Bitwise Shift: The binary representation of x is shifted to the right by the calculated shift count.
  5. Zero-Fill: The bits shifted off to the right are discarded. The vacated bits on the left (the most significant bits) are strictly filled with 0. This differs from the sign-propagating right shift (>>=), which fills vacated bits with a copy of the original sign bit.
  6. Assignment: The resulting 32-bit unsigned integer is assigned back to the variable x.

Behavior Examples

Positive Integer Shift

When shifting positive integers, >>>= behaves identically to >>=, except the result is explicitly typed as an unsigned 32-bit integer.
let a = 20; 
// 20 in 32-bit binary: 00000000000000000000000000010100

a >>>= 2; 
// Shifts right by 2, fills left with 0s
// Resulting binary:    00000000000000000000000000000101

console.log(a); // 5

Negative Integer Shift

Because negative numbers are represented using two’s complement and have a leading 1 as the sign bit, applying >>>= to a negative number results in a drastic value change. The zero-fill operation strips the negative sign, converting the underlying bit sequence into a large positive unsigned integer.
let b = -9;
// -9 in 32-bit binary: 11111111111111111111111111110111

b >>>= 2;
// Shifts right by 2, fills left with 0s
// Resulting binary:    00111111111111111111111111111101

console.log(b); // 1073741821

BigInt Exception

Attempting to use >>>= with a BigInt operand will throw a TypeError. Unsigned right shifts are incompatible with BigInt because BigInt represents an arbitrary-precision integer and does not have a fixed bit-width or a defined “most significant bit” to zero-fill.
let c = 10n;

c >>>= 2n; 
// TypeError: BigInts have no unsigned right shift, use >> instead

Non-Integer Coercion

If the left operand evaluates to a non-numeric type that cannot be parsed into a valid number, ToNumeric resolves it to NaN. During the ToUint32 conversion phase, NaN (as well as null and undefined) is coerced to 0.
let d = "Hello"; // ToNumeric yields NaN, ToUint32 yields 0
d >>>= 2;
console.log(d); // 0

let e = null; // ToNumeric yields 0, ToUint32 yields 0
e >>>= 5;
console.log(e); // 0
Master JavaScript with Deep Grasping Methodology!Learn More