9

What does the JavaScript >>> operator do?

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

How do we use it?

Pacerier
  • 76,400
  • 86
  • 326
  • 602

2 Answers2

12

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.

Olical
  • 32,800
  • 11
  • 50
  • 75
  • +1 looking at the same page :) – Matti Lyra Apr 19 '11 at 12:57
  • 3
    That last line is a little misguided. They're not *particularly* slow in modern implementations and you'll often find that they're a faster alternative to less concise methods. – Andy E Apr 19 '11 at 13:00
  • Ah, I was going on what Douglas Crockford said in `JavaScript: The good parts`. He says to avoid them because of their speed. You are probably right though, V8 seems to be quick at just about everything. – Olical Apr 19 '11 at 13:56
8

It's the zero-fill right shift operator (as opposed to the sign-propagating right shift, >>).

NPE
  • 438,426
  • 93
  • 887
  • 970