Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double more-than symbol in JavaScript

Tags:

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]&&1||(0))
}
like image 569
bcm Avatar asked Apr 03 '11 07:04

bcm


People also ask

What does >= mean in JavaScript?

The greater than or equal operator ( >= ) returns true if the left operand is greater than or equal to the right operand, and false otherwise.

What is $$ in JavaScript?

$ and $$ are valid variable names in JavaScript, they have no special meaning. Usually they set their value to library instances, in your example if you check the closure call, at the end of the file you'll see that $ is jQuery in this case if it is defined and $$ is cytoscape.

What is binary operator in JavaScript?

Bitwise operators treat its operands as a set of 32-bit binary digits (zeros and ones) and perform actions. However, the result is shown as a decimal value.


3 Answers

>> 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

like image 102
Mårten Wikström Avatar answered Sep 22 '22 04:09

Mårten Wikström


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.

like image 42
amosrivera Avatar answered Sep 23 '22 04:09

amosrivera


(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.

like image 36
Phssthpok Avatar answered Sep 24 '22 04:09

Phssthpok