- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find XOR of two number without using XOR operator in C++

In this problem, we are given integer values A & B. Our task is to *find XOR of two numbers without using the XOR operator*.

**Let's take an example to understand the problem,**

Input : A = 4, B = 5 Output : 1

## Solution Approach

One method to solve the problem is by converting the numbers to their respective binary numbers and then performing bitwise operations based on this table.

A | B | Output |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

This will return the result. For this we will use bitwise operations.

## Example

Program to illustrate the working of our solution

#include <iostream> using namespace std; int calcXORwoOperator(int a, int b){ int xorVal = 0; for (int i = 31; i >= 0; i--){ bool val1 = a & (1 << i); bool val2 = b & (1 << i); bool xorBit = (val1 & val2) ? 0 : (val1 | val2); xorVal <<= 1; xorVal |= xorBit; } return xorVal; } int main(){ int a = 4, b = 5; cout<<"XOR of the numbers is "<<calcXORwoOperator(a, b); return 0; }

## Output

XOR of the numbers is 1

**Alternated method** −

Another method to find the xor is the comparison between the bits present in both numbers one by one and performing operations equivalent to XOR on them.

The expression (a | b) - (a & b) is equivalent to a^b. So, we will perform this operation. Thereby finding XOR of two numbers a & b.

## Example

Program to illustrate the working of our solution

#include <iostream> #include <bitset> using namespace std; int calcXORwoOperator(int a, int b) { return (a | b) - (a & b); } int main(){ int a = 4; int b = 5; cout<<"The XOR of both numbers is "<<(bitset<8>(calcXORwoOperator(a, b))); return 0; }

## Output

The XOR of both numbers is 00000001

- Related Articles
- Java program to swap two numbers using XOR operator
- Importance of XOR operator in Java?
- Find the Number of Unique Triplets Whose XOR is Zero using C++
- MySQL XOR operator with IN clause?
- What is JavaScript Bitwise XOR (^) Operator?
- What is Bitwise XOR Operator (^) in JavaScript?
- Find elements of array using XOR of consecutive elements in C++
- Find number of pairs in an array such that their XOR is 0 using C++.
- What is Bitwise XOR Assignment Operator (^=) in JavaScript?
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- Maximum XOR of Two Numbers in an Array in C++
- XOR Cipher in C++
- Find a value whose XOR with given number is maximum in C++
- XOR of a subarray in C++
- Minimum number of elements to be removed to make XOR maximum using C++.