What the XOR operator does
The bitwise XOR operator, written ^, compares two integers one bit at a time. In each bit position, the result is 1 when the two bits differ and 0 when they match. XOR stands for “exclusive or,” meaning one input or the other, but not both.
For a single pair of bits, the rule is:
| First bit | Second bit | First ^ second |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
For whole numbers, the operator applies this rule to every bit position at once. Take 5 (binary 0101) and 3 (binary 0011):
0101 (5)
^ 0011 (3)
------
0110 (6)
So 5 ^ 3 is 6.
A first example
#include <iostream>
int main() {
unsigned int a = 5;
unsigned int b = 3;
unsigned int result = a ^ b;
std::cout << "5 ^ 3 = " << result << '\n'; // prints 6
return 0;
}
Output:
5 ^ 3 = 6
Useful properties
A few rules make XOR easier to reason about:
x ^ 0givesx, because no bits change.x ^ xgives 0, because every bit matches itself.- The order does not matter:
a ^ bgives the same result asb ^ a. - Applying the same value twice undoes the change:
(a ^ b) ^ bgivesa.
The last rule is why XOR appears in toggling and reversible transforms.
Compound assignment with ^=
The ^= operator computes the XOR and stores the result back in the left-hand variable. It works like the other compound forms covered in c assignment operators.
unsigned int flags = 0b1010;
flags ^= 0b0010; // flips bit 1, so flags is now 0b1000
Toggling bits
Toggling is the most common practical use. XOR with a mask flips exactly the bits that are set in the mask and leaves the others alone. Doing it again flips them back, so the operation undoes itself.
Finding the value that appears once
A classic exercise gives you a list in which every value appears twice except one. XOR-ing all the values cancels each pair, because x ^ x is 0, and leaves the unique value.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {4, 1, 2, 1, 2};
int unique = 0;
for (int n : numbers) {
unique ^= n;
}
std::cout << "Unique value: " << unique << '\n'; // prints 4
return 0;
}
Output:
Unique value: 4
The trick depends on every other value appearing an even number of times. If other values appear an odd number of times, the result is the XOR of those values, which is not a single value you can read off.
XOR is not logical XOR
C++ has no ^^ operator. The ^ operator works on bits, not on true and false values. For two bool values, a != b gives the same answer as a logical XOR:
bool a = true;
bool b = false;
bool differ = (a != b); // true
You can write ^ with bool operands, but the result is an int, not a bool, because the operands are promoted to int first. In C++, xor is a built-in alternative spelling of ^, so a xor b means the same as a ^ b.
Swapping two variables
Three XOR assignments can swap two variables without a temporary:
a ^= b;
b ^= a;
a ^= b; // a and b have swapped values
This works for two separate variables. It is harder to read than a temporary variable or std::swap, and it fails if both names refer to the same variable, because the value becomes 0. For everyday code, std::swap is the clearer choice.
Watch the operator precedence
The equality checks from c comparison operators bind more tightly than ^, so x ^ 1 == 0 means x ^ (1 == 0), not (x ^ 1) == 0. Add parentheses when you mix them:
bool is_one = ((x ^ 1) == 0); // true only when x is 1
Choose unsigned types
Bit work is easier to reason about with unsigned types. If the signed vs unsigned in c difference is new to you, review it before you apply bit operators to signed values. Small types such as unsigned char are promoted to int before ^ runs, so the result can be wider than the operands. For the sizes of each type, see primitive data types in c.
A note on XOR encryption
Repeated XOR with a key scrambles data, and applying the same key again restores it. This makes XOR a popular teaching example. A short, repeated key is easy to break, though, so do not use a plain XOR cipher to protect real data.
Summary
The ^ operator returns 1 in each bit position where two integers differ. It is useful for toggling bits, canceling pairs, and some swap tricks. Use unsigned types for bit work, and add parentheses when ^ appears next to ==.