
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Bitwise XOR in C++
The XOR operator(^), also known as "exclusive OR", is one of the types of bitwise operators, which compares each bit of the first operand to the corresponding bit of the second operand and returns 1 if both bits are different, else returns 0 if both are the same.
This only works with integral data types like int, char, short, long, and unsigned int, etc, and cannot be directly used with float, double, string, and class/struct objects, etc.
Syntax
Here is the following syntax of Bitwise XOR.
result = operand1 ^ operand2;
Where operand1 and operand2 are the integral types (like int, char, etc.)
- Returns 1 if both bits are different.
- Returns 0 if both bits are the same.
XOR Truth Table
Here is the following table, the bitwise XOR Truth Table.
Bit A |
Bit B |
Result (A | B) |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 0 |
1 | 1 | 1 |
Bit-by-bit level comparison
int a = 2; // Binary representation: 0010
int b = 5; // Binary representation: 0101
a = 0010
b = 0101
a ^ b = 0111
Where 0111 equals 7 in decimal, therefore 2 ^ 5 = 7.
Example of Bitwise XOR
Here is the following basic example code for XOR.
#include <iostream> using namespace std; int main() { int a1 = 5, b1 = 2; cout << "a1 ^ b1 = " << (a1 ^ b1) << endl; unsigned short a2 = 0x5555; // Binary: 0101 0101 0101 0101 unsigned short b2 = 0x2222; // Binary: 0010 0010 0010 0010 cout << "a2 ^ b2 = " << (a2 ^ b2) << endl; return 0; }
Output
a1 ^ b1 = 7 a2 ^ b2 = 30583
Explanation
- First, the binary representation of 5 and 2 is 0101 and 0010, respectively, therefore, 5 ^ 2 equals 7 (0111).
- In the second, 0x represents hexadecimal, where each hex digit consists of 4 bits.
- 0x5 = 0101, So; 0x5555 = 0101 0101 0101 0101
- Similarly, 0x2 = 0010, So; 0x2222 = 0010 0010 0010 0010
a2: 0101 0101 0101 0101
b2: 0010 0010 0010 0010
a2 ^ b2: 0111 0111 0111 0111
which is equal to 0x7777 in hexadecimal and 30583 in decimal
Using XOR Finding the Non-Duplicated Element in an Array
In this, we will see how we can find the unique or non-duplicated element of a given array using XOR.#include <iostream> using namespace std; int find_element(int arr[], int size) { int result = 0; for (int i = 0; i < size; i++) { result ^= arr[i]; // XOR all elements } return result; } int main() { int arr[] = {4, 3, 2, 7, 3, 4, 2}; int size = sizeof(arr) / sizeof(arr[0]); cout << "The element that appears only once is: " << find_element(arr, size) << endl; return 0; }
Output
The element which appears only once is: 7
Explanation
- Here, first, created a function named find_element taking an array and a size.
- In a for loop, the result ^= arr[i] operation has been performed, where
a ^ a = 0 (when XORing a number with itself results in 0)
a ^ 0 = a (when XORing a number with 0 results in the number itself) - So, if an element appears twice, its XOR will itself cancel it out.
- And if the element that appears only once will remain as a result, because it does not have a duplicate to cancel it out.