C++ Bitwise XOR

C++ Bitwise XOR Operator is used to perform XOR operation between the respective bits of given operands.

Syntax

The syntax for Bitwise XOR operation between x and y operands is

x ^ y

The operands can be of type int or char. Bitwise XOR operator returns a value of type same as that of the given operands.

The following table illustrates the output of XOR operation between two bits.

bit1bit2bit1 ^ bit2
000
011
101
110

Examples

In the following example, we take integer values in x and y, and find the bitwise XOR operation between x and y.

main.cpp

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int y = 9;
    int result = x ^ y;
    cout << "Result : " << result << endl;
}

Output

Result : 12
Program ended with exit code: 0

In the following example, we take char values in x and y, and find the bitwise XOR operation between x and y.

main.cpp

#include <iostream>
using namespace std;

int main() {
    char x = 'a';
    char y = '9';
    char result = x ^ y;
    cout << "Result : " << result << endl;
}

Output

Result : X
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned what Bitwise XOR Operator is, its syntax, and how to use this operator in C++ programs, with the help of examples.