Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
What is unsigned Right Shift Operator (>>>) in JavaScript?
The unsigned right shift operator (>>>) shifts the binary representation of a number to the right by a specified number of positions, filling the leftmost bits with zeros regardless of the original sign bit.
Syntax
result = number >>> positions
How It Works
Unlike the signed right shift (>>) which preserves the sign bit, the unsigned right shift always fills with zeros from the left. This treats the number as an unsigned 32-bit integer.
Example with Positive Number
<html>
<body>
<script>
var a = 14;
var b = 2; // Shift right two bits
document.write("Original number: " + a + "<br>");
document.write("Shift positions: " + b + "<br>");
document.write("(a >>> b) => ");
result = (a >>> b);
document.write(result);
</script>
</body>
</html>
Original number: 14 Shift positions: 2 (a >>> b) => 3
Example with Negative Number
<html>
<body>
<script>
var a = -14;
var b = 2; // Shift right two bits
document.write("Original number: " + a + "<br>");
document.write("Shift positions: " + b + "<br>");
document.write("(a >>> b) => ");
result = (a >>> b);
document.write(result);
</script>
</body>
</html>
Original number: -14 Shift positions: 2 (a >>> b) => 1073741820
Comparison: Signed vs Unsigned Right Shift
| Operator | Fill Bits | Preserves Sign | Example: -14 >> 2 | Example: -14 >>> 2 |
|---|---|---|---|---|
>> (Signed) |
Sign bit (1 for negative) | Yes | -4 | N/A |
>>> (Unsigned) |
Always zeros | No | N/A | 1073741820 |
Key Points
- Always fills with zeros from the left, regardless of the original sign
- Treats numbers as unsigned 32-bit integers
- Useful for converting negative numbers to positive equivalents
- Different from signed right shift (
>>) which preserves the sign bit
Conclusion
The unsigned right shift operator (>>>) performs bitwise right shifting while always filling with zeros, making it useful for unsigned arithmetic operations and converting negative numbers to their unsigned equivalents.
Advertisements
