---
title: "How does the bitwise XOR (^) operator work?"  
description: "How does the bitwise XOR (^) operator work?"  
author: "Steilla Mitchel"  
published: 2023-10-30  
updated: 2023-10-31  
canonical: https://www.mindstick.com/forum/160337/how-does-the-bitwise-xor-operator-work  
category: "javascript"  
tags: ["javascript", "bitwise operators"]  
reading_time: 2 minutes  

---

# How does the bitwise XOR (^) operator work?

How does the bitwise [XOR](https://www.mindstick.com/interview/33944/bitwise-and-or-xor-and-left-right-shift-operations) (^) [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) work?

## Replies

### Reply by Aryan Kumar

The bitwise XOR (^) operator in JavaScript is used to perform an exclusive OR operation on the individual bits of two numbers. It returns a number where each bit is set (1) if the corresponding bits of the two numbers being XORed are different, and it sets the bit to 0 if the corresponding bits are the same.

Here's how the bitwise XOR operator works with a simple example:

```plaintext
let num1 = 5; // Binary representation: 00000101
let num2 = 3; // Binary representation: 00000011

let result = num1 ^ num2; // Binary representation: 00000110

console.log(result); // Output: 6
```

In this example, the XOR operation is performed on **num1** and **num2**. If the bits at the same positions in both numbers are different, the result has a 1 in that position; otherwise, it has a 0. So, **5 ^ 3** results in 6 because the bits in the binary representation differ at the 2^0 and 2^1 positions.

Remember that the XOR operator can be useful in various scenarios, such as swapping two values without using a temporary variable or toggling specific bits within a number.


---

Original Source: https://www.mindstick.com/forum/160337/how-does-the-bitwise-xor-operator-work

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
