开发者

Double more-than symbol in JavaScript

What does this: >> mean in JavaScript?

Seen in this context:

document.onkeydown = document.onkeyup = function(e,v,y,k) {
  (i=e.keyCode-37)>>2 || (keys[i] = e.type[5]&&开发者_如何学Python1||(0))
}


>> is the bitwise right shift operator.

For example: 4 >> 1 equals 2 because 4 is 100 in binary notation, which is shifted one bit to the right, giving us 10 = 2


Javascript Bitwise Operators

Left shift a << b Shifts a in binary representation b (< 32) bits to the left, shifting in zeros from the right.

Sign-propagating right shift a >> b Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off.


(i=e.keyCode-37)>>2

This code is discarding the two least significant bits of i (similar to dividing by 4), and comparing the result to zero. This will be false when the key pressed is 37-40 (arrow keys), and true otherwise.


It's the Bitwise shift operator (see here).

Now, as to exactly what it's doing here I'm not sure... I'm sure some of our larger-brained bretheren that actually finished college could help us out with that. ;^)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜