What does the >> symbol mean? On this page, there’s a line that looks like this:
var i = 0, l = this.length >> 0, curr;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s bitwise shifting.
Let’s take the number 7, which in binary is
0b000001117 << 1shifts it one bit to the left, giving you0b00001110, which is 14Similarly, you can shift to the right:
7 >> 1will cut off the last bit, giving you0b00000011which 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)
*: 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 >> 0will also make a copy of the number, instead of taking a reference to it. Although I have no idea why anyone would want that.