标签:
#include <stdio.h> #include <stdlib.h> void inplace_swap(int *x, int *y) { *y = *x ^ *y; *x = *x ^ *y; *y = *x ^ *y; } int main() { int x = 12; int y = 23; inplace_swap(&x, &y); printf("x: %d, y: %d", x, y); return 0; }
对于任意位向量, 有a^a = 0
于是 *x = *x ^ (*x ^ *y ) = (*x ^ *x) ^ *y = *y
*y = *y ^ (*x ^ *y) = *x
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/kevin_samuel/article/details/46734323