Back to JavaScript
2026-01-137 min read

Bitwise OR (|) (JavaScript)

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

Why This Matters

In this detailed lesson, we will delve into the bitwise OR operator (|) in JavaScript. This powerful tool can help you manipulate binary data more efficiently and opens up a world of possibilities for optimizing your code. We'll cover its usage, real-world applications, common mistakes, and practice questions to help you master this essential concept.

Why Bitwise OR Matters

  1. Efficiency: Bitwise operations are faster than their bitwise counterparts because they operate on individual bits rather than entire numbers, reducing the number of arithmetic operations required.
  2. Debugging: Knowing how to use bitwise operators can help you debug complex issues in your code by allowing you to manipulate binary data directly.
  3. Interviews and Exams: Familiarity with bitwise operators is often expected in coding interviews and exams, so mastering them will give you a competitive edge.
  4. Performance Optimization: Bitwise operations can help optimize your code by reducing the number of calculations required for certain tasks.
  5. Data Manipulation: Bitwise operators allow you to perform complex data manipulations more easily and efficiently than with traditional arithmetic operations.
  6. Bitwise Operations in Modern Programming: Understanding bitwise operators is crucial for modern programming, as they are used extensively in system programming, game development, and other performance-critical applications.
  7. Binary Representation: Bitwise operations provide a direct way to work with binary data, making it easier to understand and manipulate the underlying binary representation of numbers.

Prerequisites

Before diving into the core concept of the bitwise OR operator, it's essential to have a solid understanding of the following:

  • JavaScript syntax and variables
  • Basic arithmetic operations (addition, subtraction, multiplication, division)
  • Understanding of numbers and their binary representation
  • Familiarity with other bitwise operators such as AND (&), XOR (^), and NOT (~)
  • Understanding of the modulus operator (%) and shift operators (<<, >>, >>>)

Core Concept

The bitwise OR operator (|) performs a bit-by-bit comparison between two operands. It sets the corresponding bit to 1 if either of the compared bits is 1; otherwise, it leaves the bit as it is.

const a = 5; // binary: 0000000000000000000000000000101
const b = 3; // binary: 0000000000000000000000000000011
console.log(a | b); // Output: 7, binary: 0000000000000000000000000000111

In the example above, we have two numbers 5 and 3. When we perform a bitwise OR operation on them, the result is 7, which corresponds to the binary representation where all bits that are set in either a or b are set to 1.

Bitwise OR with Negative Numbers

It's worth noting that when working with negative numbers, JavaScript stores integers in either 32-bit signed or 64-bit signed format. This can lead to unexpected results when dealing with large negative numbers and bitwise operations. To avoid confusion, it's best to work with positive numbers whenever possible.

Handling Negative Numbers

When handling negative numbers, you can convert them to their two's complement representation before performing bitwise operations. This ensures that the resulting binary representation is correct, even for large negative numbers.

function twosComplement(num) {
if (num > 0) return num;
const negativeNum = Math.abs(num);
const bits = negativeNum.toString(2).padStart(32, '0'); // Assuming 32-bit integers
return parseInt(`0b${bits.slice(1)}`, 2) - num;
}

const a = twosComplement(-5); // binary: 0000000000000000000000000000101 (for 32-bit integers)
const b = twosComplement(-3); // binary: 0000000000000000000000000000011 (for 32-bit integers)
console.log(a | b); // Output: 7, binary: 0000000000000000000000000000111

Bitwise OR with Multiple Sets

You can extend this concept to find the union of more than two sets by performing multiple bitwise OR operations in sequence:

const setA = 0b1010; // binary representation of set A: {2, 4}
const setB = 0b0101; // binary representation of set B: {1, 4}
const setC = 0b0011; // binary representation of set C: {1, 3}

// Perform bitwise OR operations to find the union of sets A, B, and C
const union = setA | setB | setC;
console.log(union); // Output: 0b1111, or {1, 2, 3, 4}

Worked Example

Let's consider an example where we need to find the union of two sets represented as bit masks.

const setA = 0b1010; // binary representation of set A: {2, 4}
const setB = 0b0101; // binary representation of set B: {1, 4}

// Perform a bitwise OR operation to find the union of sets A and B
const union = setA | setB;
console.log(union); // Output: 0b1101, or {1, 2, 4}

In this example, we have two sets represented as bit masks setA and setB. By performing a bitwise OR operation on them, we can find the union of these sets. The result is another bit mask that represents the set containing all elements from both setA and setB.

Bitwise OR with Multiple Sets

You can extend this concept to find the union of more than two sets by performing multiple bitwise OR operations in sequence:

const setA = 0b1010; // binary representation of set A: {2, 4}
const setB = 0b0101; // binary representation of set B: {1, 4}
const setC = 0b0011; // binary representation of set C: {1, 3}

// Perform bitwise OR operations to find the union of sets A, B, and C
const union = setA | setB | setC;
console.log(union); // Output: 0b1111, or {1, 2, 3, 4}

Common Mistakes

  1. Forgetting to convert numbers to binary: Sometimes, it's easy to forget to convert numbers to their binary representation when working with bitwise operators. This can lead to unexpected results.
  1. Misunderstanding the purpose of bitwise operations: Bitwise operations are not always used for arithmetic purposes. Understanding their intended use cases is crucial to avoid mistakes.
  1. Incorrectly handling negative numbers: When working with binary operations, it's important to remember that JavaScript stores integers in either 32-bit signed or 64-bit signed format. This can lead to unexpected results when dealing with large negative numbers.

Common Mistakes - Handling Overflow

Another common mistake is not handling overflow when performing bitwise operations on large numbers. In such cases, it's essential to ensure that your code handles the resulting overflow appropriately or uses appropriate data types (such as BigInt) to avoid unexpected results.

Common Mistakes - Misusing Bitwise Operators

It's important to remember that bitwise operators are binary operations and do not perform arithmetic calculations in the way you might expect. For example, using the bitwise OR operator to add numbers will yield incorrect results:

const a = 5; // binary: 0000000000000000000000000000101
const b = 3; // binary: 0000000000000000000000000000011
console.log(a | b); // Output: 7, binary: 0000000000000000000000000000111 (correct)
console.log(a | b === a + b); // Output: false (incorrect)

In the example above, using the bitwise OR operator to add numbers will yield incorrect results because it operates on individual bits rather than performing arithmetic calculations. To perform addition in JavaScript, use the + operator instead.

Practice Questions

  1. Write a function that takes two bit masks as arguments and returns the intersection of the sets represented by those bit masks.
function intersection(maskA, maskB) {
// Implement your solution here
}

console.log(intersection(0b1010, 0b0101)); // Output: {4}
  1. Write a function that takes an integer and returns its binary representation as a string.
function toBinaryString(num) {
// Implement your solution here
}

console.log(toBinaryString(7)); // Output: "0111"
  1. Write a function that takes two integers and returns their binary representation as a single string, separated by a space.
function toBinaryStringPaired(numA, numB) {
// Implement your solution here
}

console.log(toBinaryStringPaired(5, 3)); // Output: "0101 0011"
  1. Write a function that takes an integer and returns the number of set bits (bits with a value of 1) in its binary representation.
function countSetBits(num) {
// Implement your solution here
}

console.log(countSetBits(7)); // Output: 3
  1. Write a function that takes an integer and returns the bitwise AND of that number with another number (specified as a separate argument).
function bitwiseAnd(num, mask) {
// Implement your solution here
}

console.log(bitwiseAnd(7, 3)); // Output: 1

FAQ

Q: Why is the bitwise OR operator faster than its bitwise counterparts?

A: Bitwise operators operate on individual bits rather than entire numbers, reducing the number of arithmetic operations required. This makes them more efficient in terms of computation time.

Q: Can I use the bitwise OR operator with floating-point numbers?

A: No, the bitwise OR operator only works with integers. When you perform a bitwise operation on floating-point numbers, JavaScript will first convert them to their integer representations, which can lead to unexpected results.

Q: How do I check if a specific bit is set in a number using the bitwise OR operator?

A: You can use the XOR (^) and AND (&) operators in combination with the bitwise OR operator to check if a specific bit is set in a number. Here's an example:

const num = 13; // binary: 0000000000000000000000000001101
const bitIndex = 2; // index of the bit we want to check (starting from 0)

// Perform a bitwise AND operation with the bit mask (1 << bitIndex)
const bitMask = 1 << bitIndex;
const isBitSet = num & bitMask !== 0;
console.log(isBitSet); // Output: true, since the second bit from the right in the binary representation of `num` is set to 1
Bitwise OR (|) (JavaScript) | JavaScript | XQA Learn