版權聲明:本文可能為部落客原創文章,若标明出處可随便轉載。 https://blog.csdn.net/Jailman/article/details/80180470
異或交換兩個數的值是資源開銷最小的方法,不需要中介數,原理簡單的來說就是異或的負負得正。
代碼:
#include <stdio.h>
int main()
{
int a = 11, b = 22;
printf("a=%d b=%d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("a=%d b=%d\n", a, b);
}
編譯:
gcc test.c -o test
執行:
a=11 b=22
a=22 b=11