#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
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-16 16:20:04