如果 a ^ b = c,那么c ^ b = a, c ^ a = b(穷举证明)0 ^ 1 = 1, 1 ^ 0 = 1, 0 ^ 0 = 0, 1 ^ 1 = 0
1 2 3 4 5 6 7 8 9 10 11 12 13
voidXOR_test(void){ int a = 5, b = 7; a ^= b; b ^= a; a ^= b; }/* a = 7, b = 5 a = 0101 b = 0111 a = a ^ b = 0101 ^ 0111 = 0010; == 2 b = a ^ b = 0010 ^ 0111 = 0101; == 5 a = a ^ b = 0010 ^ 0101 = 0111; == 7 */
<<左移运算符(>>右移同理)
a << b意思是吧a左移b位高位丢弃,低位补零。实际上,左移n位,就等于把这个数✖️2^n。但是左移运算比乘法快很多。同时编译器规定移动操作时,符号位会根据原符号位补上右移等于把这个数➗2^n(向下取整)。