C++ xor Keyword
Example
The xor keyword gives the same result as the ^ operator:
int x = 5;
int y = 3;
cout << (x ^ y) << "\n";
cout << (x xor y);
Definition and Usage
The xor keyword is another way to write the ^ bitwise operator, which is called exclusive or. Both ways work the same, but ^ is much more common.
The ^ operator compares the bits (the 1s and 0s that a number is stored as) of two numbers. Each bit in the result is 1 if the two bits are different, and 0 if they are the same. For example, 5 is 101 in binary and 3 is 011, so 5 ^ 3 is 110, which is 6.
In C++, x ^ 2 does not mean x to the power of 2. Use pow(x, 2) for that.
Related Pages
Read more about operators in our C++ Operators Tutorial.
The pow() function raises a number to the power of another number.