使用位运算, 利用几个性质 a ^ a = 0 a ^ 0 = a
1 #include<stdio.h> 2 int main(void){ 3 int a = 18, b = 20; 4 a = a ^ b; 5 b = a ^ b; 6 a = a ^ b; 7 printf("%d %d\n", a, b); 8 return 0; 9 }
再来一个
#include<stdio.h> int main(void){ int a = 8, b = 9; //交换a,b a = a + b; b = a - b; a = a - b; printf("%d %d\n", a, b); return 0; }