left shift ( << )

right shift ( >> )

unsigned right shift ( >>> )

Navigation Aids -- This Page | Navigation Aids -- This Topic | Navigation Aids -- This Site | ||
---|---|---|---|---|

- left shift operator
- right shift operator
- unsigned right shift operator
- integer operand
- precedence
- associativity
- binary operator
- NaN
- infinity
- The Binary Bitwise Logical Operators are covered separately. The ECMAScript Language Specification addresses them separately. They also have a much lower precedence.
- Synonyms:
- Zero Fill Right Shift: This is another term used for the Unsigned Right Shift Operator.
- Signed Right Shift: This is another term used for the Right Shift Operator.

This page will show the basic rules for JavaScript bitwise shift operator group. In addition, type conversions are discussed where applicable. Some special situation cases are explained. And finally, some scripting examples are provided with comments.

- The bitwise shift operators are binary operators. They have a "left-to-right" associativity and a precedence of 11. There operands are or convert to integers.
- The left shift operator will shift all bits to the left for the integer specified in the first operand by the number of positions specified in the integer of the second operand.
- The right shift operator will shift all bits to the right for the integer specified in the first operand by the number of positions specified in the integer of the second operand. This operator will left fill with the sign bit.
- The unsigned right shift operator will shift all bits to the right for the integer specified in the first operand by the number of positions specified in the integer of the second operand. This operator will left fill with zeros.
- The second operand should be an integer between 0 and 31.

- The bitwise operators will return unpredictable results when one or both of the operands are not integers.
- The bitwise operators will truncate any bits above 32 for integers that are too large to fit in a 32-bit representation (2147483648 and over).
- You can use the left shift operator to multiply by powers of 2:
- Shift left 1 position is the same as multiplying by 2; e.g. 7 << 1 yields 14.
- Shift left 2 positions is the same as multiplying by 4; e.g. 6 << 2 yields 24.
- etc.

- You can use the right shift operator to divide by powers of 2 (discard any remainder):
- Shift right 1 position is the same as dividing by 2; e.g. 7 >> 1 yields 3.
- Shift right 2 positions is the same as dividing by 4; e.g. 6 >> 2 yields 1.
- etc.

- Negative numbers will become positive numbers with the unsigned right shift operator because the bits shifted in on the left are always zero. This destroys the negative sign bit (bit 32). The sign conversion can make for value large positive values.
- For non-negative numbers, the two right shift operators yield the same result.
- Shifting left 31 positions will yield zero for all numbers.
- Shifting right 31 positions will yield zero for positive numbers and -1 for negative numbers.

For simplicity, we use only one byte in our examples. But the bitwise shift operators work with four bytes (32 bits).

Expression | Giving | Comment |
---|---|---|

x = 4; x << 2 |
16 | left shift 00000100 shifted to the left two bits giving 00010000; same as:: 4 * 2 ^{2} |

x = -5; x << 2 |
-20 | left shift 11111011 shifted to the left two bits giving 11101100 |

x = 16; x >> 2 |
4 | right shift with sign extended 00010000 shifted to the right two bits and left filled with sign bit giving 00000100 |

x = -9; x >> 2 |
-3 | right shift with sign extended 11110111 shifted to the right two bits and left filled with sign bit giving 11111101 |

x = 16; x >>> 4 |
1 | right shift with zero fill 00010000 shifted to the right four bits and left zero filled giving 00000001 |

x = -10; x >>> 3 |
30 | right shift with zero fill 11110110 shifted to the right three bits and left zero filled giving 00011110 |