Back to JavaScript
2026-02-286 min read

Bitwise XOR assignment (^=) (JavaScript)

Learn Bitwise XOR assignment (^=) (JavaScript) step by step with clear examples and exercises.

Title: Mastering Bitwise XOR Assignment (^=) in JavaScript

Why This Matters

Understanding bitwise operations, including the Bitwise XOR assignment operator (^=), is crucial in programming as it allows for efficient problem-solving and optimized code. With the help of this operator, you can perform various tasks like toggling bits, checking if numbers have an odd number of set bits, and implementing hash functions. This lesson will guide you through the basics of using the bitwise XOR assignment in JavaScript, with real-world examples and common mistakes to avoid.

Prerequisites

Before diving into the core concept, it's essential to have a good understanding of the following topics:

  1. Basic JavaScript syntax
  2. Variables and data types
  3. Arithmetic operators
  4. Control structures (loops, conditional statements)
  5. Understanding binary representation of numbers
  6. Familiarity with bitwise operators (&, |, ~, <<, >>)
  7. Recursion (for understanding the XOR function)

Core Concept

The Bitwise XOR assignment operator (^=) performs a bitwise exclusive OR operation on the operands and assigns the result to the left operand. In other words, it compares each corresponding bit of both operands and sets the output bit to 1 if one or both input bits are 1, otherwise sets the output bit to 0.

Here's a simple example:

let a = 5; // binary: 00000101
let b = 3; // binary: 00000011

a ^= b; // a becomes 6, binary: 00000110

In the above example, we have two numbers (5 and 3) represented in binary. When we perform the bitwise XOR operation, we get a new number (6), where each bit is calculated based on the corresponding bits of the original numbers.

Bitwise Operations and Number Representation

To better understand how bitwise operations work, let's take a closer look at the binary representation of numbers. Each digit in the binary system corresponds to a power of 2 (starting from 0), with the rightmost digit representing 2^0, the next digit representing 2^1, and so on. For example:

Number: 5
Binary: 00000101

In this case, each bit represents a power of 2:

  • The rightmost set bit (1) corresponds to 2^0 (1)
  • The second bit from the right set bit (1) corresponds to 2^1 (2)
  • The third bit from the right unset bit (0) corresponds to 2^2 (0)
  • The fourth bit from the right unset bit (0) corresponds to 2^3 (0)

Bitwise Operations on Numbers

Now that we understand the binary representation of numbers, let's explore how bitwise operations work. Here are some examples:

  1. Bitwise AND (&): Compares each corresponding bit and sets the output bit to 1 only if both input bits are 1.
let a = 5; // binary: 00000101
let b = 3; // binary: 00000011
a & b; // Output: 1, binary: 00000001
  1. Bitwise OR (|): Sets the output bit to 1 if one or both input bits are 1.
let a = 5; // binary: 00000101
let b = 3; // binary: 00000011
a | b; // Output: 7, binary: 00000111
  1. Bitwise NOT (~): Flips all bits in a number.
let a = 5; // binary: 00000101
~a; // Output: -7, binary: 11110111
  1. Bitwise Left Shift (<<): Shifts all bits in a number to the left by a specified number of positions. The vacated bits are filled with zeros on the right side.
let a = 5; // binary: 00000101
a << 1; // Output: 10, binary: 00001010
  1. Bitwise Right Shift (>>): Shifts all bits in a number to the right by a specified number of positions. The vacated bits are filled with zeros on the left side.
let a = 5; // binary: 00000101
a >> 1; // Output: 2, binary: 00000010

Worked Example

Let's consider a practical example where we need to implement a function that checks if a given number is odd or even using the Bitwise XOR operator:

function isOdd(num) {
return (num ^ (num >> 1)) !== 0;
}

console.log(isOdd(5)); // true
console.log(isOdd(6)); // false

In the above example, we have a function isOdd() that takes an integer as input and returns a boolean indicating whether the number is odd or even. The magic happens inside the function, where we perform the bitwise XOR operation between the number and its right-shifted version (num >> 1). If the number is odd, the result will be non-zero; otherwise, it will be zero.

Common Mistakes

  1. Forgetting to shift the number: Remember that when using the bitwise XOR operator for checking if a number is odd or even, you should always right-shift the number by 1 (num >> 1) to divide it by 2.
function isOdd(num) {
return (num ^ num / 2) !== num / 2; // incorrect implementation
}
  1. Misunderstanding the result of the XOR operation: It's essential to understand that the bitwise XOR operation returns 0 if both bits are the same and 1 if they are different. This can lead to confusion when trying to solve problems like finding the parity (odd or even) of a number.
  1. Using the wrong operator: Be careful not to confuse the bitwise XOR assignment operator (^=) with the bitwise XOR operator (^). The former performs an operation and assigns the result, while the latter returns the result without affecting the original values.

Common Mistakes - Additional Examples

  1. Using the wrong operator in the assignment: Instead of using the bitwise XOR assignment operator (^=), you might accidentally use the bitwise XOR operator (^) when trying to perform an operation and assign the result to a variable.
let a = 5; // binary: 00000101
let b = 3; // binary: 00000011
a ^= b + 2; // Incorrect usage of the assignment operator for addition
console.log(a); // Output: 6, but should be 8 (binary: 00001000)
  1. Misusing the bitwise XOR assignment operator for addition: The bitwise XOR operator is not suitable for adding numbers because it treats each bit as a separate entity. Instead, you should use the standard addition operator (+).
let a = 5; // binary: 00000101
let b = 3; // binary: 00000011
a ^= b + 2; // Incorrect usage of the assignment operator for addition
console.log(a); // Output: 6, but should be 8 (binary: 00001000)

Practice Questions

  1. Write a JavaScript function that toggles all bits in a given number. For example, if the input is 5 (binary: 00000101), the output should be 9 (binary: 00001001).
function toggleBits(num) {
// Your code here
}

console.log(toggleBits(5)); // Output: 9
  1. Implement a function that checks if two numbers have an odd number of common set bits. For example, the numbers 3 (binary: 00000011) and 5 (binary: 00000101) have one common set bit, so the function should return true.
function hasOddCommonBits(num1, num2) {
// Your code here
}

console.log(hasOddCommonBits(3, 5)); // Output: true
console.log(hasOddCommonBits(4, 6)); // Output: false

FAQ

  1. What's the difference between the bitwise XOR operator (^) and the bitwise XOR assignment operator (^=)?
  • The bitwise XOR operator (^) performs an operation and returns the result without affecting the original values. On the other hand, the bitwise XOR assignment operator (^=) performs an operation and assigns the result to the left operand.
  1. Can I use the bitwise XOR operator for adding two numbers?
  • No, the bitwise XOR operator is not suitable for adding numbers because it treats each bit as a separate entity. Instead, you should use the standard addition operator (+).
  1. What are some common applications of the bitwise XOR assignment operator in JavaScript?
  • The Bitwise XOR assignment operator can be used to implement hash functions, toggle bits in a number, and check if a number is odd or even. Additionally, it can be useful for solving problems that require manipulating individual bits in a number.
  1. Is it possible to use the bitwise operators for complex mathematical operations?
  • While bitwise operators are primarily used for manipulating individual bits in numbers, they can sometimes be used creatively to solve complex mathematical problems. However, using them for such purposes is generally considered unconventional and may make your code harder to read and understand.
Bitwise XOR assignment (^=) (JavaScript) | JavaScript | XQA Learn