方法一:位运算
原理图:
利用位运算中的异或操作(相同是0 不同是1)
1 int a = 10; 2 int b = 20; 3 a = a^b; 4 b = a^b; 5 a = a^b;
方法二:
普通方法计算:
左侧代码:
1 int a = 20; 2 int b = 10; 3 a = a+b; 4 b = a-b; 5 a = (a-b)/2; 6 b = (a+b)/2;
右侧代码:
1 int a = 20; 2 int b = 10; 3 a = a+b; 4 b = a-b; 5 a = a-b;