不创建临时变量交换两个变量的内容
(使用按位异或操作符)
^ 按位异或(相同为0,相异为1)
#include <stdio.h>
int main()
{
int a = 3;
int b = 5;
printf("交换前:a=%d b=%d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("交换后:a=%d b=%d", a, b);
return 0;
}
不创建临时变量交换两个变量的内容
(使用按位异或操作符)
^ 按位异或(相同为0,相异为1)
#include <stdio.h>
int main()
{
int a = 3;
int b = 5;
printf("交换前:a=%d b=%d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("交换后:a=%d b=%d", a, b);
return 0;
}