class XorArithmetic { static void Main() { XorArithmetic.UseNumericType(); int a = 10; int b = 6; Console.WriteLine("before invoke ChangeNumeric method a:{0},b:{1}", a, b); XorArithmetic.ChangeNumeric(ref a, ref b); Console.WriteLine("After invoke ChangeNumeric method a:{0},b:{1}", a, b); } static void UseNumericType() { Console.WriteLine("当运算整数时"); //使用XOR 运算符判断数字是否相等 Console.WriteLine("是否相等:{0}", (6 ^ 6) == 0 ? true : false); //当0与任何数异或 Console.WriteLine("0与任何数异或:{0}", 0 ^ 20); //当1与任何数异或 Console.WriteLine("1与任何数异或:{0}", 1 ^ 11); } static void UseBoolType() { Console.WriteLine("当运算bool操作数时"); //bool类型异或,只要有一个操作数为TRUE时才为true Console.WriteLine("true ^ false:{0}", true ^ false); Console.WriteLine("true ^ true:{0}", true ^ true); Console.WriteLine("false ^ false:{0}", false ^ false); } static void ChangeNumeric(ref int a,ref int b) { a = a ^ b; b = b ^ a; a = a ^ b; } }