Skip to content

shift operators (<<, >>, >>>)

In JavaScript, the shift operators (<<, >>, >>>) move the bits of their left operand left or right by the number of positions in their right operand. For number operands, they work on a 32-bit integer and return a 32-bit integer, which is unsigned only for >>>.

Before shifting a number, the operators truncate the left operand to a 32-bit integer and drop any higher bits. They then use only the lowest five bits of the right operand, so a shift count is always between 0 and 31. The << and >> operators also accept two bigint operands, but >>> doesn’t.

JavaScript Shift Operators Syntax

Each shift operator takes the value to shift on the left and the shift count on the right:

Language: JavaScript Syntax
<value> << <count>
<value> >> <count>
<value> >>> <count>

The three operators differ in which bits they shift in:

Operator Name Example Result
<< Left shift 9 << 2 36
>> Right shift -9 >> 2 -3
>>> Unsigned right shift -9 >>> 2 1073741821

The << operator fills with zeros from the right. The >> operator copies the sign bit in from the left, while >>> fills with zeros and always returns a non-negative number.

Pick an operator, an operand, and a shift count to see which bits each operator drops and which it shifts in:

Interactive diagram — enable JavaScript to view.

Setting the count to 32 gives the same result as a count of 0, because only the lowest five bits of the count are used.

JavaScript Shift Operators Examples

Say a color picker stores a color as one integer, such as 0x1e90ff, and the code needs its red, green, and blue channels. Right shifts move the red and green channels into the lowest eight bits:

Language: JavaScript
> const color = 0x1e90ff;
undefined

> color >> 16
30
> (color >> 8) & 0xff
144
> color & 0xff
255

The bitwise AND with 0xff keeps only the lowest eight bits, which removes the red channel from the green result.

Say a string hash function returns the signed 32-bit integer -1640531535, and a cache needs it as an eight-digit hex key. Shifting by zero with >>> reinterprets the same bits as an unsigned value:

Language: JavaScript
> const hash = -1640531535;
undefined

> hash.toString(16)
'-61c8864f'
> (hash >>> 0).toString(16)
'9e3779b1'

The >> 0 form would keep the value signed, because >> preserves the sign bit.

JavaScript Shift Operators Gotchas

  • Shifting a 1 into the sign bit makes the result negative, so 1 << 31 evaluates to -2147483648.
  • The shift count wraps at 32, so 5 << 32 evaluates to 5, not 0.
  • Numbers larger than 32 bits lose their high bits before the shift, so x >> 1 isn’t a safe way to halve values above 2 ** 31 - 1.
  • The >>> operator throws a TypeError for bigint operands, and mixing a bigint with a number throws for all three operators.

JavaScript Shift Operators Use Cases

  • Packing and unpacking color channels or other fields stored in one integer
  • Building single-bit masks such as 1 << 3 for flag sets
  • Converting a signed 32-bit result to an unsigned one with >>> 0
  • Multiplying or dividing integers by powers of two in hashing and checksum code
A developer pulls a Math.floor() lever on a counting machine with a coffee sack on its scale, a big dial, a % gauge and a toFixed(2) receipt printer.

Course

JavaScript Numbers for Pythonistas

Port a coffee roastery's batch planner from Python to JavaScript, and learn to divide, round, read numbers from text, and format them with JavaScript's one number type.

basics js py

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 12, 2026