Zero-Fill Right Shift (>>>)
What you'll learn: How the >>> operator shifts bits right while always filling with zeros, treating numbers as unsigned.
What Makes >>> Different
You've already learned >>, which preserves the sign bit when shifting right. The zero-fill right shift (>>>) takes a different approach: it always fills the leftmost bits with zeros, regardless of the number's sign. This effectively treats the number as an unsigned 32-bit integer.
How It Works
The >>> operator shifts all bits to the right by a specified number of positions, and fills empty positions on the left with zeros:
let num = 8; // Binary: 00000000000000000000000000001000
let result = num >>> 2;
// Result: 2 // Binary: 00000000000000000000000000000010
The Unsigned Behavior
Here's where it gets interesting with negative numbers:
let negative = -8; // Binary: 11111111111111111111111111111000
let shifted = negative >>> 2;
// Result: 1073741822 // Binary: 00111111111111111111111111111110
Notice the result is a huge positive number! That's because >>> filled the left with zeros, removing the sign bit entirely.
Practical Analogy
Think of a conveyor belt carrying 32 boxes. With >>, when boxes fall off the right, you replace the left boxes by copying the first box's color. With >>>, you always replace left boxes with white boxes (zeros), no matter what color was there before.
Common Use Cases
// Converting to unsigned 32-bit integer
let unsigned = -1 >>> 0; // Result: 4294967295
// Extracting specific bits
let byte = 0xFF00 >>> 8; // Result: 255
Key Takeaway: The >>> operator shifts bits right and always fills with zeros, converting negative numbers to large positive values by treating them as unsigned 32-bit integers.