The >>
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".
a >> b
This 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".
For example, 9 >> 2
yields 2:
. 9 (base 10): 00000000000000000000000000001001 (base 2) -------------------------------- 9 >> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
Likewise, -9 >> 2
yields -3
, because the sign is preserved:
. -9 (base 10): 11111111111111111111111111110111 (base 2) -------------------------------- -9 >> 2 (base 10): 11111111111111111111111111111101 (base 2) = -3 (base 10)
9 >> 2; // 2 -9 >> 2; // -3
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Bitwise Shift Operators' in that specification. |
Desktop | ||||||
---|---|---|---|---|---|---|
Bitwise right shift (a >> b ) |
1 | 12 | 1 | 3 | 3 | 1 |
Mobile | ||||||
---|---|---|---|---|---|---|
Bitwise right shift (a >> b ) |
1 | 18 | 4 | 10.1 | 1 | 1.0 |
Server | |
---|---|
Bitwise right shift (a >> b ) |
0.1.100 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Right_shift