## Introduction to C++ XOR operator

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, and 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 is zero and the result of XOR operation on the given two bits of the two operands is zero if the two bits of the given two operands is one and 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.

**Syntax of XOR operator in C++**

`variable1 ^ variable2`

where variable1 is the first variable containing the first operand on whose bits XOR operation must be performed,

^ symbol represents the XOR operation

variable2 is the second variable containing the second operand on whose bits XOR operation must be performed.

### 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 is 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 is 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 mention 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;
}
```

**Output:**

In the above program, we have included the header file iostream using cin and cout statements in the program. Then the main method is called within which two integer variables a and b are defined to store the two integer values on whose bits the XOR operation is to be performed. Then the integer variable c is defined to store the result of the XOR operation, which is displayed as the output on the screen.

#### 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;
}
```

**Output:**

In the above program, we have included the header file iostream using cin and cout statements in the program. Then the main method is called within which two integer variables a and b are defined to store the two integer values on whose bits the XOR operation is to be performed. Then the integer variable c is defined to store the result of the XOR operation, which is displayed as the output on the screen.

#### 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;
}
```

**Output:**

In the above program, we have included the header file iostream using cin and cout statements in the program. Then the main method is called within which two integer variables a and b are defined to store the two integer values on whose bits the XOR operation is to be performed. Then the integer variable c is defined to store the result of the XOR operation, which is displayed as the output on the screen.

### Recommended Articles

This is a guide to the C++ XOR operator. Here we discuss the concept of XOR operator in C++ through definition, syntax, and usage of XOR operator through programming examples and their outputs. You can also look at the following article to learn more –

Â Â 4 Online CoursesÂ |Â Â 5 Hands-on Projects |Â Â 37+ Hours |Â Â Verifiable Certificate of Completion

4.5

View Course

Related Courses