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))
}
>> 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. ;^)
Related
This question already has answers here:
Javascript's Shift right with zero-fill operator (>>>) yielding unexpected result
(2 answers)
Closed 1 year ago.
From the documentation:
The right shift operator (>>) shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left. Since the new leftmost bit has the same value as the previous leftmost bit, the sign bit (the leftmost bit) does not change. Hence the name "sign-propagating".
From what I understand, since 100 is 0b1100100, shifting it 100 times to the right should yield 0b0. However, when I run 100 >> 100 in Javascript (using chrome), it returns 6. Why is this the case? I am guessing it has something to do with JS's internal representation of numbers but would like to know more clearly.
Edit: The answer is still 6, even when using the unsigned >>> operator. Sign/unsigned does not seem to matter.
Unsigned operation documentation:
The unsigned right shift operator (>>>) (zero-fill right shift) shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left. The sign bit becomes 0, so the result is always non-negative. Unlike the other bitwise operators, zero-fill right shift returns an unsigned 32-bit integer.
The value to the right is taken mod 32. (i.e. Only the last five bits are used.) If you calculate 100>>32, you get 100, which is the same thing you get when you compute 100>>0. After that, 100>>33 is 50, and the cycle repeats.
As i know,
Sign-propagating right shift (a >> b) : Shifts a in binary representation b bits to the right, discarding bits shifted off.
Ex: 8>>2 will return 2.because binary 1000 will shift 2 times right and return 0010.
Zero-fill right shift (a >>> b): Shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.
Ex: 8>>2 return 2.it also retun the same.
then what is difference between >> and >>> operator and why javascript has these two operator instead of one or if i am wrong then please guide me to get right concept?
The bitwise operators assume their operands are 32-bit signed integers.
00000000000000000000000000001000 in base 2 equals 8 in base 10.
In 8 >> 2, the sign-propagating shift-right operator (>>) shifts the binary number two places, preserving the sign (which is the first bit):
00000000000000000000000000000010 in base 2 equals 2 in base 10.
In 8 >>> 2, the zero-fill right-shift operator (>>>) shifts the binary number two places, filling in the left bits with 0s:
00000000000000000000000000000010 in base 2 equals 2 in base 10
These are identical, simply because the first bit for positive binary numbers is a zero.
From MDN:
For non-negative numbers, zero-fill right shift and sign-propagating
right shift yield the same result.
For negative numbers, however, things look different:
11111111111111111111111111111000 in base 2 equals -8 in base 10.
In -8 >> 2, the sign-propagating shift-right operator (>>) shifts the binary number two places, preserving the sign:
11111111111111111111111111111110 in base 2 equals -2 in base 10.
In -8 >>> 2, the zero-fill right-shift operator (>>>) shifts the binary number two places, filling in the left bits with 0s:
00111111111111111111111111111110 in base 2 equals 1073741822 in base 10.
This question already has answers here:
What is the JavaScript >>> operator used for? [duplicate]
(2 answers)
Closed 9 years ago.
In filter documentation page in Mozilla website, I saw >>> operator:
var t = Object(this),
len = t.length >>> 0, //here
res, thisp, i, val;
if (typeof fun !== 'function') {
throw new TypeError();
}
Here you can find the complete document: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
What's this operator and what it does?
It's a bit shift operator.
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators,
a >>> b shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.
That doesn't explain why anyone would bother to shift a value zero bits to the right, though. You might as well multiply it by one, or add zero.
As others explained, it is the "bitwise shift with zero" operator.
With positive values this has the same effect as the normal >> operator. With negative values, the most-significant bit is the "sign" bit. Normal shifting will shift the sign bit in (1 for negative values, 0 for positive). >>> has a different effect, because it always shifts in a zero instead of the sign bit:
-2>>1 == -1
-2>>>1 == 2147483647
More on how negative values are represented can be found here.
What all shift operators do is cast the value to a 32-bit integer (at least my Firefox does), so shifting by 0 means that the value will always be within the 32-bit range. Bitwise shift with 0 will also make sure the value is positive:
a = Math.pow(2,32) // overflow in 32-bit integer
a>>0 == 0
b = Math.pow(2,32) - 1 // max 32-bit integer: -1 when signed, 4294967295 when unsigned
b>>0 == -1
b>>>0 == 4294967295 // equal to Math.pow(2,32)-1
Called Bitwise Shift Right with Zero Operator. This operator is just like the >> operator, except that the bits shifted in on the left are always zero.
For example: (A >>> 1) is 1.
http://www.tutorialspoint.com/javascript/javascript_operators.htm
Update:
This explains what bitwise shift operators do: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_shift_operators
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.
I am implementing decoding of BER-compressed integers and recently I've found a weird JavaScript behavior related to bitwise operations with big integers.
E.g.:
var a = 17516032; // has 25 bits
alert(a << 7) // outputs -2052915200
alert(a * 128) // outputs 2242052096
alert(2242052096 >> 16) // outputs -31325
alert(2242052096 / 65536) // outputs 34211
While the first workaround (multiplication instead of left shift) is acceptable, the second isn't.
Why it happens? How to bear with it?
17516032 in binary is 00000001000010110100011000000000. Shifting to the left by 7 gives you 10000101101000110000000000000000. This is equal to -2052915200 in two's complement (which is how almost all computers represent negative numbers).
>> is a signed right shift. That means that the leftmost bit (which determines the sign of a number) will be shifted into the left side.
e.g.
1100 >> 2 == 1111
0111 >> 2 == 0001
If you want to do an unsigned shift (which ignores the sign bit), use >>> which will zero-fill the left end of the bitstring.
Bitwise operators work on 32 bit integers, while multiplication and division works on floating point numbers.
When you shift a number, it's converted from a floating point number to a 32 bit integer before the operations, and converted back to a floating point number after the operation. The number 2242052096 has the 32nd bit set, so it is a negative number when converted to and from a 32 bit integer.
The >> right shift operator doesn't change the sign of the value, i.e. the bits that are shifted in from the left have the same value as the sign bit. Use the >>> right shift operator to shift in zero bits instead.
Reference: MDN: Bitwise operators
(2242052096 / 65536) == (2242052096 >>> 16)
Note the different shift.
Javascript normally represents numbers as (double-precision) floating point.
Almost all bitwise operations convert to a signed 32-bit integer, do whatever they're going to do, then treat the result as a signed 32-bit integer when converting back.
The exception is >>> which treats the result as an unsigned 32-bit integer when converting back.
So:
right shifts can be made to work simply by using >>> instead of >> ;
a * 128 gives the expected answer because it's never converted to a signed 32-bit integer in the first place - it's just a floating-point multiplication;
a << 7 gives an unexpected answer because it's converted to a signed 32-bit integer, and then you shift a 1 into the sign bit, resulting in a negative signed 32-bit value.
There isn't a <<<, but if you want to write your left shift as a shift, you can use
(a << 7) >>> 0
to get the expected answer (the >>> 0 effectively casts the signed 32-bit value to an unsigned 32-bit value).