# Use of C++ XOR operator  Written by Sagar RabidasJanuary 28, 2022
C++
1 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 1 VIEWS SHARE Sagar Rabidas

Software Developer

## C++ XOR operator

The xor operator is one of the bitwise operators in c++, which takes two operators as the operands and on every little bit of the two operands. The xor operation is performed, and the result of the xor operation at the given two bits of the 2 operands is 0. If the 2 bits of the given two operands is 0 and the result of xor operation on the given two bits of the two operands is 0 if the 2 bits of the given two operands is one and the result of xor operation on the given two bits of the 2 operands is one if one of the two bits of the given two operands is 0 or one and the alternative bit is one or 0, this is if the 2 bits of the given two operands are exclusive, the result is one.

#### Syntax of XOR operator in C++

`variable1 ^ variable2`

## Working of XOR operator in C++

• The XOR operator is one of the bitwise operators in C++, which takes two operators as the operands, and on each bit of the two operands, the XOR operation is performed.
• The result of the XOR operation on the given two bits of the two operands is zero if the two bits of the given two operands are zero.
• The result of the XOR operation on the given two bits of the two operands is zero if the two bits of the given two operands are one.
• The result of XOR operation on the given two bits of the two operands is one if one of the two bits of the given two operands is zero or one and the other bit is one or zero, that is, if the two bits of the given two operands are different, the result is one.

## Examples of C++ XOR operator

Different examples are mentioned below:

### Example #1

C++ program to demonstrate the XOR operator in C++ to perform XOR operation on the given two operands and display the result:

#### Code:

`//The header iostream is included to be able to make use of cin and cout statements`
`#include <iostream>`
`using namespace std;`
`//main method is called`
`int main()`
`{`
`//an integer variable called a is defined to store the first integer value`
`int a = 5;`
`//an integer variable called b is defined to store the second integer value`
`int b = 4;`
`//an integer variable called c is defined to store the result of XOR operation on the two values stored in the variables a and b`
`int c = a ^ b;`
`cout << "The result after performing XOR operation on a and b is " << c;`
`return 0;`
`}`

### Example #2

C++ program to demonstrate the XOR operator in C++ to perform XOR operation on the given two operands and display the result:

#### Code:

`//The header iostream is included to be able to make use of cin and cout statements`
`#include <iostream>`
`using namespace std;`
`//main method is called`
`int main()`
`{`
`//an integer variable called a is defined to store the first integer value`
`int a = 20;`
`//an integer variable called b is defined to store the second integer value`
`int b = 35;`
`//an integer variable called c is defined to store the result of XOR operation on the two values stored in the variables a and b`
`int c = a ^ b;`
`cout << "The result after performing XOR operation on a and b is " << c;`
`return 0;`
`}`

### Example #3

C++ program to demonstrate the XOR operator in C++ to perform XOR operation on the given two operands and display the result:

#### Code:

`//The header iostream is included to be able to make use of cin and cout statements`
`#include <iostream>`
`using namespace std;`
`//main method is called`
`int main()`
`{`
`//an integer variable called a is defined to store the first integer value`
`int a = 100;`
`//an integer variable called b is defined to store the second integer value`
`int b = 35;`
`//an integer variable called c is defined to store the result of XOR operation on the two values stored in the variables a and b`
`int c = a ^ b;`
`cout << "The result after performing XOR operation on a and b is " << c;`
`return 0;`
`}`

C
C++
C++ XOR operator
1 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 1 VIEWS SHARE     You must be Logged in to comment
Code Block Sagar Rabidas
Software Developer     +8 more
300 Blog Posts
Trending Technologies
15 Software40 DevOps46 Frontend Development24 Backend Development20 Server Administration17 Linux Administration26 Data Center24 Sentry24 Terraform23 Ansible83 Docker70 Penetration Testing16 Kubernetes21 NGINX20 JenkinsX17
Recommended Blogs
8        8 Anik Adhikary Anik Adhikary Anik Adhikary Anik Adhikary Anik Adhikary Anik Adhikary Anik Adhikary Anik Adhikary Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.