Understanding Bitwise Shifts in JavaScript: << and >>

A left shift multiplies a number by 2 per position, and a right shift divides it by 2. How JavaScript's bitwise shift operators work, with binary examples.

Trevor I. Lasn Trevor I. Lasn
4 min read

Bitwise operators are powerful but often overlooked features in JavaScript. While they’re not common in everyday web development, understanding them can unlock performance optimizations and help you better understand how computers handle numbers at a low level.

What Are << and >>?

<< (left shift) and >> (right shift) are operators that move bits left or right in a number’s binary representation.

JavaScript
// Left Shift (<<)
let num = 5; // Binary: 00000101
let shifted = num << 1; // Binary: 00001010 (decimal: 10)
// Right Shift (>>)
let num2 = 10; // Binary: 00001010
let shifted2 = num2 >> 1; // Binary: 00000101 (decimal: 5)

Left Shift <<: Quick Multiplication

Left shifting multiplies a number by 2 for each position shifted:

JavaScript
console.log(5 << 1); // 10 (5 * 2)
console.log(5 << 2); // 20 (5 * 4)
console.log(5 << 3); // 40 (5 * 8)

Right Shift >>: Integer Division

Right shifting divides by 2 for each position shifted, rounding down:

JavaScript
console.log(10 >> 1); // 5 (10 / 2)
console.log(10 >> 2); // 2 (10 / 4)
console.log(10 >> 3); // 1 (10 / 8)

Practical Applications of Bit Shifts

Bit shifts shine in several specific domains where working with binary data becomes essential. Let’s dig into each use case and understand exactly how and why they work.

When building permission systems or configuration managers, bit shifts provide an elegant way to handle multiple boolean flags in a single number. Think of each bit position as a switch - it’s either on (1) or off (0).

The most common and practical use of bit operations in JavaScript is working with numbers. Here’s a real example you might encounter:

JavaScript
// Parse RGB colors to hex and back
function rgbToHex(r, g, b) {
return ((r << 16) | (g << 8) | b).toString(16).padStart(6, '0');
}
function hexToRgb(hex) {
const num = parseInt(hex, 16);
return {
r: num >> 16 & 255,
g: num >> 8 & 255,
b: num & 255
};
}
// Use case: Working with CSS colors
console.log(rgbToHex(255, 0, 0)); // 'ff0000'
console.log(hexToRgb('ff0000')); // { r: 255, g: 0, b: 0 }

When working with images in JavaScript, each pixel’s color is often represented as a single number. Bit shifts make it easy to work with these values.

JavaScript
function getRedChannel(pixel) {
// Move red bits to the right and mask off other colors
return (pixel >> 16) & 0xFF;
}
function getGreenChannel(pixel) {
return (pixel >> 8) & 0xFF;
}
function getBlueChannel(pixel) {
return pixel & 0xFF;
}
function createPixel(red, green, blue) {
// Combine RGB values into a single number
return (red << 16) | (green << 8) | blue;
}
// Using in practice
const pixel = createPixel(255, 128, 0); // Bright orange color
console.log(getRedChannel(pixel)); // 255
console.log(getGreenChannel(pixel)); // 128
console.log(getBlueChannel(pixel)); // 0

This code is doing something quite clever. Instead of storing three separate numbers for red, green, and blue, we pack them all into a single number. Each color gets 8 bits of space - enough to store values from 0 to 255. The red value goes in the leftmost 8 bits, green in the middle 8 bits, and blue in the rightmost 8 bits.

When we want to get a color back out, we shift the bits right until our desired color is in the lowest 8 bits, then mask off everything else with & 0xFF

It’s like having a bookshelf where each shelf can hold numbers up to 255, and we’re just moving books between shelves.

This bit-shifting approach is much faster than storing RGB values in an array or object. It’s especially useful when processing lots of pixels, like in image filters or games. Modern browsers use similar techniques under the hood when rendering graphics.

That said, you might never need to write code like this yourself. Modern web APIs usually handle these low-level details for you. But understanding how it works helps you appreciate what’s happening behind the scenes in your browser’s rendering engine.

The truth is, for most web development work, you’ll rarely need bit operations. They’re most useful when:

  • Converting between number systems (like RGB colors)
  • Working with binary data (like WebGL or file processing)
  • Implementing performance-critical use cases like game physics engines or real-time data processing

For everyday tasks, using objects or Sets are usually clearer and more maintainable.


Trevor I. Lasn

Trevor I. Lasn

Building whiteheap.com and 0xinsider.com.



Related Articles

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

JavaScript
7 min read

AggregateError holds several errors in one, the way Promise.any() reports that every promise rejected. How to throw and read it, plus the new SuppressedError.

Sep 2, 2024
Read article
JavaScript
4 min read

How the Suspense component manages async dependencies and improves loading states in Vue apps

Aug 23, 2024
Read article
JavaScript
3 min read

Use the &&= operator to safely update truthy values while preserving falsy states

Nov 5, 2024
Read article
JavaScript
8 min read

How to handle unique values and key-value pairs properly without type coercion and performance issues

Nov 17, 2024
Read article
JavaScript
3 min read

Float16Array stores 16-bit floating point numbers in half the memory of a Float32Array. How it works, when to use it and which browsers support it.

Apr 14, 2025
Read article
JavaScript
7 min read

How was JavaScript written back in the day? I dug into early frameworks and libraries to see how code looked then and what still holds up.

Jun 12, 2025
Read article
JavaScript
4 min read

The 'with' statement is the only feature ever deprecated in JavaScript

Aug 22, 2024
Read article
JavaScript
6 min read

Peaks.js is a client-side JavaScript component to display and interact with audio waveforms in the browser

Oct 22, 2019
Read article
JavaScript
5 min read

The Stage 2 pipeline operator passes a value through a chain of calls left to right, with % marking where it goes, instead of nesting h(g(f(x))).

Oct 29, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/bitwise-shifts-in-javascript. It was written by a human and polished using grammar tools for clarity.