C++ Bitwise XOR Assignment

In C++, Bitwise XOR Assignment Operator is used to compute the Bitwise XOR operation of left and right operands, and assign the result back to left operand. In this tutorial, we will learn how to use Bitwise XOR Assignment operator in C++, with examples.

The syntax to compute bitwise XOR a value of 2 and value in variable x, and assign the result back to x using Bitwise XOR Assignment Operator is

x ^= 2

Example

In the following example, we take a variable x with an initial value of 9, add bitwise XOR it with value of 2, and assign the result to x, using Bitwise XOR Assignment Operator.

main.cpp

#include <iostream>
using namespace std;

int main() {
    int x = 9;
    x ^= 2;
    cout << "x : " << x << endl;
}

Output

x : 11
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned about Bitwise XOR Assignment Operator in C++, with examples.