9

I have this piece of javascript code that I am trying to understand

return ( n >>> 0 ) * 2.34e10;

So what does >>> mean?

And thanks in advance ... this is my first question on SO

var x
  • 135
  • 6

2 Answers2

18

It's a zero-fill right shift. This won't do anything to positive whole numbers or 0, but it does funny things on negative numbers (because the most significant bit changes to zero).

 2 >>> 0 === 2
 1 >>> 0 === 1
 0 >>> 0 === 0
-1 >>> 0 === 4294967295
-2 >>> 0 === 4294967294
-3 >>> 0 === 4294967293

It should be noted (thanks Andy!) that bit shifting in JavaScript converts the arguments to signed 32-bit integers before doing the shifting. Therefore >>> 0 essentially does a Math.floor on positive numbers:

1.1 >>> 0 === 1
1.9 >>> 0 === 1
Skilldrick
  • 69,215
  • 34
  • 177
  • 229
  • I wanted to +1, but I felt compelled to perform a minor edit first ;-) You could also expand on what it will do to positive floats, if you wanted to :-) – Andy E Sep 17 '10 at 10:37
  • 1
    @Andy Very appreciated, and good point. – Skilldrick Sep 17 '10 at 10:37
  • Brilliant!!... Thanks a lot Skilldrick, Andy E and acqu13sce. Thanks also for the references. – var x Sep 17 '10 at 10:58
  • 1
    This operation is actually the only bitwise operation in JavaScript that is **unsigned** – Joe Dec 15 '11 at 22:33
  • `>>>` is unsigned right shift, `>>` is signed right shift. The former converts the RHS using ToUint32, which does what it's called. – gsnedders Dec 15 '11 at 22:34
1

It's a bitwise operator. It means shift n by 0 bits. Not sure what it's trying to do in the instance you show.

a >>> b  // shift a by b bits to the right, padding with zeros
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
acqu13sce
  • 3,789
  • 4
  • 25
  • 32