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.

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

Tech executive with over a decade of experience programming and building products. Building 0xinsider.com, ceodumps.com and whiteheap.com.



Related Articles

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

JavaScript
19 min read

Common causes of JavaScript memory leaks in Node.js and Deno, such as global objects and callbacks that hold large data, and how to profile heap usage.

Aug 10, 2024
Read article
JavaScript
5 min read

scheduler.postTask() runs JavaScript at user-blocking, user-visible or background priority, giving finer control than setTimeout(0) over when work runs.

Nov 26, 2024
Read article
JavaScript
4 min read

Understanding the new import attributes syntax and why we can't rely on file extensions alone

Nov 10, 2024
Read article
JavaScript
4 min read

Stop mixing try/catch with Promise chains - JavaScript's new Promise.try handles return values, Promises, and errors uniformly

Nov 10, 2024
Read article
JavaScript
7 min read

JavaScript has eight falsy values: false, 0, -0, 0n, an empty string, null, undefined and NaN. Empty arrays and objects are truthy. The edge cases, explained.

Oct 27, 2024
Read article
JavaScript
6 min read

The creator of JavaScript and Node.js are challenging Oracle's control over the JavaScript name

Nov 23, 2024
Read article
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
3 min read

Copy and paste text, images, and files using the new navigator.clipboard API

Dec 7, 2024
Read article
JavaScript
4 min read

Why the new Error.isError() method solves important cross-realm issues and provides more reliable error identification than instanceof

May 9, 2025
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.