Back to Web Development
2026-04-025 min read

JS Bitwise (Web Development)

Learn JS Bitwise (Web Development) step by step with clear examples and exercises.

Title: Mastering JavaScript Bitwise Operations for Web Development

Why This Matters

In web development, optimizing code is crucial to ensure smooth performance and fast loading times. JavaScript bitwise operations are a powerful tool that can help you write more efficient code by performing certain operations faster than their corresponding mathematical counterparts. Understanding bitwise operations will not only make your code run quicker but also make it easier to debug and troubleshoot issues.

Prerequisites

Before diving into JavaScript bitwise operations, you should have a basic understanding of the following topics:

  • JavaScript syntax and variables
  • Basic arithmetic operators (e.g., +, -, \*, /)
  • Control structures (e.g., if-else, loops)
  • Understanding of number representation in binary format

Number Representation

In JavaScript, numbers are represented as 64-bit floating point values, but for the purposes of bitwise operations, they can be treated as 32-bit integers (from -2^31 to 2^31 - 1). It is essential to understand binary representation when working with bitwise operations.

Core Concept

Bitwise operations manipulate individual bits within a number instead of the whole number itself. The six bitwise operators in JavaScript are:

  • & (AND)
  • | (OR)
  • ^ (XOR)
  • ~ (NOT or complement)
  • << (left shift)
  • >> (right shift)

Let's take a closer look at each operator.

AND (&)

The AND operator returns 1 if both bits in the corresponding positions are 1, and 0 otherwise. For example:

6 & 3 = 0b110 & 0b011 = 0b010 (2 in binary)

OR (|)

The OR operator returns 1 if at least one of the bits in the corresponding positions is 1, and 0 otherwise. For example:

6 | 3 = 0b110 | 0b011 = 0b111 (7 in binary)

XOR (^)

The XOR operator returns 1 if the bits in the corresponding positions are different, and 0 otherwise. For example:

6 ^ 3 = 0b110 ^ 0b011 = 0b101 (5 in binary)

NOT (~)

The NOT operator flips all the bits of a number. For example:

~6 = 11111110 (binary, decimal: 64 - 6)

Left Shift (<<)

The left shift operator moves the bits of a number to the left by a specified number of positions. For example:

5 << 2 = 0b0101 << 2 = 0b101000 (binary, decimal: 20)

Right Shift (>>)

The right shift operator moves the bits of a number to the right by a specified number of positions. If the leftmost bit is shifted out, it will be filled with 0 (arithmetic right shift) or the sign bit (logical right shift). For example:

5 >> 2 = 0b0101 >> 2 = 0b0001 (binary, decimal: 1, arithmetic right shift)
-5 >> 2 = 11110111 >> 2 = 11110110 (-6 in binary, logical right shift)

Worked Example

Let's say we have two numbers: x = 10 (binary: 0b1010) and y = 5 (binary: 0b0101). We want to perform various bitwise operations on them.

// AND
let result = x & y;
console.log(result); // 2 (binary: 0b010)

// OR
let result = x | y;
console.log(result); // 15 (binary: 0b1111)

// XOR
let result = x ^ y;
console.log(result); // 3 (binary: 0b0101)

// NOT
let result = ~x;
console.log(result); // -10 (decimal, binary: 11111010)

// Left shift
let result = x << 2;
console.log(result); // 40 (binary: 0b101000)

// Right shift (arithmetic right shift)
let result = x >> 2;
console.log(result); // 1 (binary: 0b0001)

Common Mistakes

  • Forgetting to convert numbers to integers before performing bitwise operations (e.g., 5 & "6" will result in a TypeError).
  • Using the wrong operator for the desired operation (e.g., using | instead of & or vice versa).
  • Not understanding the difference between arithmetic right shift and logical right shift.
  • Failing to convert binary strings to integers before performing bitwise operations.

Common Mistakes (cont'd)

  • Neglecting to handle edge cases when using bitwise operators in more complex scenarios.
  • Overlooking the importance of understanding binary representation for effective use of bitwise operations.

Practice Questions

  1. Write a function that checks if a number is even using bitwise operations.
  2. Implement a function that swaps two numbers without using a temporary variable.
  3. Write a function that calculates the sum of two numbers using only bitwise XOR and bitwise AND.
  4. Given two integers, write a function that returns their binary representation as a string.
  5. Implement a function that determines whether a number is a power of 2 using bitwise operations.
  6. Write a function that finds the first set bit (the rightmost non-zero bit) in a given 32-bit integer.
  7. Create a function that counts the number of set bits (bits with value 1) in a given 32-bit integer.
  8. Implement a function that rotates a 32-bit integer to the left or right by a specified number of positions.
  9. Write a function that compares two strings using bitwise XOR and checks if they are anagrams of each other.
  10. Create a function that finds the maximum number among three numbers using only bitwise operations.

FAQ

Q: What's the difference between arithmetic right shift and logical right shift?

A: Arithmetic right shift fills the leftmost bit with the sign bit, while logical right shift fills it with 0.

Q: Why are bitwise operations useful in web development?

A: Bitwise operations can help optimize code by performing certain operations faster than their mathematical counterparts. They can also make code easier to debug and troubleshoot issues.

Q: Can I use bitwise operators with floating-point numbers in JavaScript?

A: No, bitwise operators only work with integers (32-bit or less). If you need to perform bitwise operations on larger numbers, consider using BigInt.

Q: How can I convert a binary string to an integer in JavaScript?

A: To convert a binary string to an integer, use the parseInt() function with a radix of 2 (e.g., parseInt("1010", 2)).

Q: What is the fastest way to find the first set bit in a given number?

A: The fastest method to find the first set bit in a given number involves using a combination of right shifts and AND operations. This technique is known as the "find first set" or "popcount" algorithm.

Q: How can I count the number of set bits (bits with value 1) in a given number?

A: Counting the number of set bits (also called the Hamming weight) can be achieved using a combination of right shifts and XOR operations. This technique is known as the "popcount" algorithm.

Q: How to implement a function that calculates the sum of two numbers using only bitwise XOR and bitwise AND?

A: To calculate the sum of two numbers using only bitwise operators, you can use the following steps:

  1. Perform an XOR operation on both numbers (sum = x ^ y). This will give you the bits where the numbers differ.
  2. Perform a AND operation between both numbers and shift the result right by one position (carry = (x & y) >> 1). The carry will contain the sum of the carry bits from the lower positions.
  3. Repeat steps 1 and 2 for higher-order bits until you reach the most significant bit.
  4. Add the final carry to the XOR result to get the sum (sum = sum + carry).
JS Bitwise (Web Development) | Web Development | XQA Learn