The exclusive or (xor)
The following table recalls the values of the exclusive or of 2 bits, denoted by xor:
0 ^ 0 = 0 |
0 ^ 1 = 1 |
1 ^ 0 = 1 |
1 ^ 1 = 0 |
In Java, or in C/C++, the binary operator ^
implements the bitwise xor on machine words. In C++, it supports every integer types:
bool, char, short, int, long et and their unsigned variants.
Example :
int i,j;
...
int k = i ^ j ;
computes in k the bitwise xor of the two integers "int" i and j.
Thus, for i=23="10111" and j=
134="10000110", we have k=
145= "10010001" as shown in the following table:
|
10111 |
23 |
(16+4+2+1) |
^ |
10000110 |
134 |
(128+4+2) |
= |
10010001 |
145 |
(128+16+1) |