不用临时变量,实现两个数的交换
#include <stdio.h>
void swap(int *a, int *b)
{
*a = *a ^ *b;
*b = *b ^ *a;
*a = *a ^ *b;
}
int main()
{
int a, b;
a = 10;
b = 100;
swap(&a, &b);
printf("%d %d", a, b);
}
a = a ^ b; (1)
b = b ^ a; (2)
a = a ^ b; (3)
b = b ^ ( a ^ b) = b ^ b ^ a = 0 ^ a = a
a = a ^ b ^ a = b;