C语言的 异或运算的 运算原理 应用。需要注意的是 位运算 是符合 交换律, 结合律 及 分配律的
1/* 2 * ==================================================================== 3 * All Rights Reserved 2007-2015 CODD Orgnization 4 * ==================================================================== 5 * Title: xor.c 6 * Desp: sample of bit xor operation 7 * Author: Liu Dongguo(jealdean@outlook.com) 8 * Verion: 1.0 9 * Created: 03/24/2015 22:09:14 PM 10 * ChgOn: 2015-03-25 01:06:45 11 * ==================================================================== 12 */ 13 14Principles 15P0 x^x=0 16P1 a^0=a 17P2 c=a^x ==> a=c^x (=a^x^x=a^0=a) 18#include <stdio.h> 19int main (int argc, char* argv[]) 20{ 21 sample1 :swap two values 22 int a=3; 23 int b=517; 24 printf("before swap:a=%d,b=%d\n",a,b); 25 a^=b^=a^=b; 26 printf("after swap:a=%d,b=%d\n",a,b); 27 samplp2: letter lowercase --> UPPERCASE 28 int cMask='a'^'A'; 29 char c='b'; 30 printf("%c-->%c\n",c,c^cMask); 31 sample3: test Law of distribution 32 if(12^22==(12^19+12^3)) 33 { 34 printf("yes,xor obey Law of distribution\n"); 35 } 36 return 0; 37}