1 按位异或
适用于:面试,嵌入式开发需要节约内存的场合
不借助中间变量,交换2个变量
有3种方法,1是相加,2是相乘,3是按位异或,其中按位异或最好,因为不会越界
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include<stdio.h> 4 #include<stdlib.h> 5 6 main() 7 { 8 int a = 100; 9 int b = 10; 10 11 int c = 200; 12 int d = 20; 13 14 int e = 300; 15 int f = 30; 16 17 printf("a=%d,b=%d ", a, b); 18 19 a = a + b; 20 b = a - b; 21 a = a - b; 22 23 printf("a=%d,b=%d ", a, b); 24 25 printf("c=%d,d=%d ", c, d); 26 27 c = c*d; 28 d = c / d; 29 c = c / d; 30 31 printf("c=%d,d=%d ", c, d); 32 33 printf("e=%d,f=%d ", e, f); 34 35 e = e^f; 36 f = e^f; 37 e = e^f; 38 39 printf("e=%d,f=%d ", e, f); 40 41 system("pause"); 42 }