Table of Contents
The bitwise XOR operator ^ compares each corresponding bit of two integer operands. A result bit is 1 when the corresponding bits are different, and 0 when they are the same.
#include <stdio.h>
int main()
{
int a = 12;
int b = 10;
printf("a ^ b = %d\n", a ^ b);
return 0;
}
Example Output #
a ^ b = 6
Explanation #
The values are represented in binary as:
12 = 1100
10 = 1010
----
0110
Each pair of corresponding bits is compared:
| a | b | a ^ b |
|---|---|---|
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
Therefore, 1100 ^ 1010 produces 0110, which is decimal 6.
The bitwise XOR operator produces 1 when the corresponding bits are different.