第一种:利用第三方变量
1 int a = 10, b = 12; 2 int temp = 0; 3 temp = a; 4 a = b; 5 b = temp; 6 Console.WriteLine( a); 7 Console.WriteLine( b);
第二种:加加减减法
1 int a = 10, b = 12; 2 a = a+b;//a=22 3 b = a-b;//22-12=10 4 a = a - b;//22-10=12 5 Console.WriteLine( a); 6 Console.WriteLine( b);
第三种:使用异或运算符
1 int a = 10, b = 12; 2 a = a ^ b;//1010^1100=0110 3 b = a ^ b;//0110^1100=1010 4 a = a ^ b;//0110^1010=1100 5 Console.WriteLine( a); 6 Console.WriteLine( b);
原文地址:https://www.cnblogs.com/flywithme/p/9998797.html
时间: 2024-11-06 12:34:24