1

What does the >> symbol mean? On this page, there's a line that looks like this:

var i = 0, l = this.length >> 0, curr;
Waleed Khan
  • 11,426
  • 6
  • 39
  • 70
chanHXC
  • 611
  • 1
  • 7
  • 17

2 Answers2

8

It's bitwise shifting.

Let's take the number 7, which in binary is 0b00000111

7 << 1 shifts it one bit to the left, giving you 0b00001110, which is 14

Similarly, you can shift to the right: 7 >> 1 will cut off the last bit, giving you 0b00000011 which is 3.

[Edit]
In JavaScript, numbers are stored as floats. However, when shifting you need integer values, so using bit shifting on JavaScript values will convert it from float to integer.

In JavaScript, shifting by 0 bits will round the number down* (integer rounding) (Better phrased: it will convert the value to integer)

> a = 7.5;
7.5
> a >> 0
7

*: Unless the number is negative.

Sidenote: since JavaScript's integers are 32-bit, avoid using bitwise shifts unless you're absolutely sure that you're not going to use large numbers.

[Edit 2]
this.length >> 0 will also make a copy of the number, instead of taking a reference to it. Although I have no idea why anyone would want that.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
  • @JanDvorak Added that to my answer – Tom van der Woerdt Dec 25 '12 at 11:17
  • 1
    Also, if the nubmer is stored as a string, it will be converted to number. – John Dvorak Dec 25 '12 at 11:17
  • Alternatives: `a||0`, `~~a` – John Dvorak Dec 25 '12 at 11:20
  • In that code they are shifting array.length to zero. I doubt that array.length ever will be fractional. So what is the point to do this there? – SergeyS Dec 25 '12 at 11:22
  • @SergeyS if `array` is not a real array then `length` could be anything. The method could could be grafted to anything. – John Dvorak Dec 25 '12 at 11:23
  • @JanDvorak But that code is added to the prototype of the Array object, so it will always be an array, right? – Tom van der Woerdt Dec 25 '12 at 11:26
  • @SergeyS No idea. In the example you provided it seems completely irrelevant. – Tom van der Woerdt Dec 25 '12 at 11:26
  • How it can be not a real array, if we are adding this function to its prototype. The only way I see is if someone replaced 'length' property for array. In that case all the code will be useless anyway, and bitwise shift does not help really) – SergeyS Dec 25 '12 at 11:27
  • @SergeyS `Array.prototype.reduce.call({length:"3.14"})` – John Dvorak Dec 25 '12 at 11:28
  • @SergeyS or even `{length:"3.14", reduce:Array.prototype.reduce}.reduce()` – John Dvorak Dec 25 '12 at 11:30
  • Looks like a nonsense, what output would be. I guess function will crash anyway – SergeyS Dec 25 '12 at 11:31
  • @SergeyS `Array.prototype.reduce.call({length:"3.14"}, function(a,b){return a+b})` should return `"undefinedundefinedundefined"` (since `{length:"3.14}[0..2]` are all `undefined`). – John Dvorak Dec 25 '12 at 11:33
  • Cool, if I ever will want 'undefinedundefinedundefined' I will be using your approach. But wait, what if I provide length equal to letter (not a fractional number), bitwise shift will help me?)) – SergeyS Dec 25 '12 at 11:35
  • @SergeyS a non-number applied in an integer context (bitwise operators) will be cast to `0`, so `length` will be zero. – John Dvorak Dec 25 '12 at 11:39
  • @SergeyS Calling array functions on pseudo-arrays is pretty common. Admittably, the pseudo-arrays won't usually be degenerate like mine is. It's still a good idea to cast the length to integer. – John Dvorak Dec 25 '12 at 11:44
  • Jan Dvorak, could you please provide answer here (not in comments) with some practical usages of this function, where length is not a whole integer. Really appreciated. – SergeyS Dec 25 '12 at 11:45
  • @SergeyS I'm not saying that casting the length to integer is actually useful. I'm just saying that length _can_ be non-integer and casting to integer might seem like a good idea to the authors of ECMAscript. – John Dvorak Dec 25 '12 at 11:47
  • @SergeyS personally, I can't think of any practical case where this is useful - you can always cast to integer yourself in your pseudo-arrays. However, it is easy for the authors of the `Array.prototype.reduce` as well, so they do so that you don't. – John Dvorak Dec 25 '12 at 11:52
1

Just like in many other languages >> operator (among << and >>>) is a bitwise shift.

Crozin
  • 43,890
  • 13
  • 88
  • 135