What does the >>
symbol mean? On this page, there's a line that looks like this:
var i = 0, l = this.length >> 0, curr;
What does the >>
symbol mean? On this page, there's a line that looks like this:
var i = 0, l = this.length >> 0, curr;
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.
Just like in many other languages >>
operator (among <<
and >>>
) is a bitwise shift.