XOR is the exclusive OR operator.
In the XOR operation, bits are compared with one another, just like the & and | operators.
When two bits are identical, XOR returns 0.
When the two bits are different, XOR returns 1.
C language XOR operator is the caret character: ^.
#include <stdio.h> char *to_binary(int n); int main() //from w ww.j av a 2s . c o m { int a,x,r; a = 73; x = 170; printf(" %s %3d\n",to_binary(a),a); printf("^ %s %3d\n",to_binary(x),x); r = a ^ x; printf("= %s %3d\n",to_binary(r),r); return(0); } char *to_binary(int n) { static char bin[9]; int x; for(x=0;x<8;x++) { bin[x] = n & 0x80 ? '1' : '0'; n <<= 1; } bin[x] = '\0'; return(bin); }
If you use the same XOR value on a variable twice, you get back the variable's original value.