//法一(正常方法)
int main()
{
int a = 3;
int b = 5;
int tmp = 0;//临时变量
printf("before:a=%d b=%d\n", a, b);
tmp = a;
a = b;
b = tmp;
printf("after:a=%d b=%d\n", a, b);
return 0;
}
//法二(加减法) 有缺陷 a=a+b超出范围就可能出错(可能会溢出)
int main()
{
int a = 3;
int b = 5;
printf("before:a=%d b=%d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("after:a=%d b=%d\n", a, b);
return 0;
}
//法三(异或法)
int main()
{
int a = 3;
int b = 5;
printf("before:a=%d b=%d\n", a, b);
a = a ^ b;//a 011 b 101 异或结果为110
b - a ^ b;
a = a ^ b;
printf("after:a=%d b=%d\n", a, b);
return 0;
}