Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the JavaScript >>> operator used for? [duplicate]

What does the JavaScript >>> operator do?

For example, alert(1 >>> 2).

How do we use it?

like image 380
Pacerier Avatar asked Apr 19 '11 12:04

Pacerier


People also ask

What does >>> mean in JavaScript?

The unsigned right shift operator ( >>> ) (zero-fill right shift) evaluates the left-hand operand as an unsigned number, and shifts the binary representation of that number by the number of bits, modulo 32, specified by the right-hand operand.

What is double tilde in JavaScript?

This is a special kind of operator in JavaScript. To understand the double tilde operator, first, we need to discuss the tilde operator or Bitwise NOT. The (~) tilde operator takes any number and inverts the binary digits, for example, if the number is (100111) after inversion it would be (011000).


1 Answers

It is a bitwise operator, here is an explanation taken from this page.

This is the zero-fill right shift operator which shifts the binary representation of the first operand to the right by the number of places specified by the second operand. Bits shifted off to the right are discarded and zeroes are added on to the left. With a positive number you would get the same result as with the sign-propagating right shift operator, but negative numbers lose their sign becoming positive as in the next example, which (assuming 'a' to be -13) would return 1073741820:

Watch out though, bitwise operators are pretty slow in JavaScript.

like image 116
Olical Avatar answered Nov 08 '22 23:11

Olical