Back to JavaScript
2025-11-297 min read

Bitwise operators (JavaScript)

Learn Bitwise operators (JavaScript) step by step with clear examples and exercises.

Why This Matters

In this lesson, we delve into the fascinating world of bitwise operators in JavaScript, a topic that can be a big help in your coding journey, whether you're preparing for an interview, debugging real-world issues, or simply honing your programming skills. Understanding bitwise operators is essential for advanced programming tasks and can help you solve complex problems more efficiently.

Prerequisites

Before diving into bitwise operators, it's important to have a solid understanding of the following topics:

  • Basic JavaScript syntax and data types (numbers, strings, booleans)
  • Arithmetic operations (+, -, *, /, %, ++, --)
  • Control structures (if, else if, switch, for, while, do-while loops)
  • Understanding of binary numbers and their representation in JavaScript
  • Familiarity with the concept of bitwise operators and their uses

Binary Numbers and JavaScript

A binary number is a base-2 number system consisting of 0s and 1s. Each digit in a binary number corresponds to a power of 2, starting from 0 (2^0) on the rightmost side, also known as the least significant bit (LSB), and increasing on the leftmost side, which is the most significant bit (MSB).

In JavaScript, numbers are represented internally as 64-bit floating-point values. However, when performing bitwise operations, JavaScript treats integers as 32-bit signed or unsigned integers, depending on the context.

Bitwise Operators in JavaScript

JavaScript provides six bitwise operators: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), and >> (right shift). These operators manipulate the individual bits within a binary number.

Core Concept

Understanding Binary Numbers and Bitwise Operations

A binary number is a base-2 number system consisting of 0s and 1s. Each digit in a binary number corresponds to a power of 2, starting from 0 (2^0) on the rightmost side, also known as the least significant bit (LSB), and increasing on the leftmost side, which is the most significant bit (MSB).

Bitwise operations manipulate these individual bits within a binary number. JavaScript provides six bitwise operators: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), and >> (right shift).

Bitwise AND (&)

The bitwise AND operator compares each corresponding bit in two binary numbers. If both bits are 1, the result is 1; otherwise, it's 0.

let a = 60; // binary: 0011 1100
let b = 13; // binary: 0000 1101
console.log(a & b); // output: 12, binary: 0000 1100

Bitwise OR (|)

The bitwise OR operator sets the corresponding bit to 1 if either of the two bits being compared is 1; otherwise, it's 0.

console.log(a | b); // output: 61, binary: 0011 1101

Bitwise XOR (^)

The bitwise XOR operator sets the corresponding bit to 1 if the two bits being compared are different; otherwise, it's 0.

console.log(a ^ b); // output: 49, binary: 0011 0011

Bitwise NOT (~)

The bitwise NOT operator flips each bit in a binary number.

let c = ~60; // binary: 1100 0011, flip all bits and output: -61
console.log(c); // output: -61

Left Shift (<<)

The left shift operator moves the bits of a binary number to the left by a specified number of positions. A 1 is added to the vacated rightmost bit position.

console.log(a << 2); // output: 240, binary: 1110 0000

Right Shift (>>)

The right shift operator moves the bits of a binary number to the right by a specified number of positions. The vacated leftmost bit position is filled with either a 0 or the sign bit (MSB) for signed integers, depending on the unsigned or signed arithmetic behavior you choose in JavaScript.

console.log(a >> 2); // output: 15, binary: 0000 1111

Arithmetic Right Shift (>>) for Signed Integers

For signed integers in JavaScript, the right shift operator fills the leftmost vacated bit with the sign bit (MSB). This can lead to unexpected results when shifting negative numbers:

let num = -12; // binary: 1000 0000
console.log(num >> 1); // output: -6, binary: 1111 1100

To fill the leftmost bit with 0 for signed integers, use the unsignedRightShift method from the Math object:

console.log(Math.imul(num >>> 1, -1)); // output: 6, binary: 0000 0110

Logical Right Shift (>>>) for Unsigned Integers

For unsigned integers in JavaScript, the logical right shift operator fills the leftmost vacated bit with a 0.

let num = 12; // binary: 0000 1100
console.log(num >>> 1); // output: 6, binary: 0000 0110

Worked Example

Let's create a simple program that uses bitwise operators to perform various operations on two numbers:

let num1 = 34; // binary: 100010
let num2 = 19; // binary: 100101

// Bitwise AND
console.log("Bitwise AND:", num1 & num2); // output: 6, binary: 110

// Bitwise OR
console.log("Bitwise OR:", num1 | num2); // output: 50, binary: 110100

// Bitwise XOR
console.log("Bitwise XOR:", num1 ^ num2); // output: 26, binary: 11010

// Bitwise NOT (num1):
let num1Not = ~num1; // binary: 1100 0011, flip all bits and output: -35
console.log("Bitwise NOT (num1):", num1Not); // output: -35

// Bitwise NOT (num2):
let num2Not = ~num2; // binary: 1011 1010, flip all bits and output: -18
console.log("Bitwise NOT (num2):", num2Not); // output: -18

// Left shift
console.log("Left shift (num1 by 2):", num1 << 2); // output: 136, binary: 1000 0000
console.log("Left shift (num2 by 1):", num2 << 1); // output: 38, binary: 1001 100

// Right shift
console.log("Right shift (num1 by 2):", num1 >> 2); // output: 8, binary: 0000 1000
console.log("Right shift (num2 by 3):", num2 >> 3); // output: 1, binary: 0000 0001

Common Mistakes

Forgetting the order of operations for bitwise operators

In JavaScript, multiplication and division have higher precedence than bitwise operators. To avoid confusion, it's a good practice to use parentheses when needed:

let a = 5;
let b = 3;
console.log(a & b * 2); // incorrect output: 10, should be 6 (first perform multiplication)
console.log((a & b) * 2); // correct output: 6

Assuming right shift with signed integers always fills the leftmost bit with 0

For signed integers in JavaScript, the right shift operator fills the leftmost vacated bit with the sign bit (MSB). This can lead to unexpected results when shifting negative numbers:

let num = -12; // binary: 1000 0000
console.log(num >> 1); // output: -6, binary: 1111 1100

To fill the leftmost bit with 0 for signed integers, use the unsignedRightShift method from the Math object:

console.log(Math.imul(num >>> 1, -1)); // output: 6, binary: 0000 0110

Not considering the arithmetic behavior of right shift for signed integers

For signed integers in JavaScript, the right shift operator fills the leftmost vacated bit with the sign bit (MSB) by default. However, you can choose to use unsigned arithmetic behavior instead, which will fill the leftmost vacated bit with 0:

let num = -12; // binary: 1000 0000
console.log(num >> 1); // output: -6, binary: 1111 1100 (default behavior)
console.log((-num >>> 1)); // output: 12, binary: 0000 1100 (unsigned behavior)

Practice Questions

  1. Write a JavaScript program that checks if a given number is even or odd using bitwise operators.
  2. Implement a function that reverses the bits of an integer using only bitwise operations.
  3. Write a program that finds the first set bit (the rightmost bit that's 1) in a binary number using bitwise AND and right shift.
  4. Create a program that swaps two numbers without using a temporary variable using bitwise XOR.
  5. Implement a function to calculate the sum of two integers using only bitwise operators and without using multiplication or division.
  6. Write a JavaScript program to find the maximum number between two given numbers using bitwise operations.
  7. Create a program that checks if a number is a power of 2 using bitwise AND and right shift.
  8. Implement a function to count the number of set bits (bits with value 1) in an integer using bitwise operators.
  9. Write a JavaScript program to check if a given number is prime using only bitwise operations.
  10. Create a program that calculates the factorial of a number using bitwise operators and without using multiplication or division.

FAQ

How can I check if a number is odd or even using bitwise operators?

To determine whether a number is odd or even using bitwise operators, you can perform a bitwise AND operation with 1 and check the result:

function isOdd(num) {
return num & 1 !== 0;
}
console.log(isOdd(5)); // true
console.log(isOdd(6)); // false

How can I reverse the bits of an integer using only bitwise operations?

To reverse the bits of an integer, you can use a combination of left and right shift operators:

function reverseBits(num) {
let reversed = 0;
for (let i = 31; i >= 0; i--) {
reversed |= num & 1 << i;
num >>= 1;
}
return reversed;
}
console.log(reverseBits(123)); // output: 8159

How can I find the first set bit (the rightmost bit that's 1) in a binary number using bitwise AND and right shift?

To find the position of the rightmost set bit, you can perform a series of right shifts and AND operations with 1:

function findFirstSetBit(num) {
let pos = 0;
while (num !== 0) {
num &= num - 1;
pos++;
}
return pos;
}
console.log(findFirstSetBit(25)); // output: 2

How can I swap two numbers without using a temporary variable using bitwise XOR?

To swap two

Bitwise operators (JavaScript) | JavaScript | XQA Learn