C#异或运算符的使用
题目描述
编写一个控制台应用,采用异或运算符,实现两个整型变量值的交换。并在Program类的Main进行验证。
输入
依次输入2个整数
输出
输出交换前、后两个变量的值
样例输入
12 78
样例输出
before exchange first=12,second=78 after exchange first=78,second=12
using System;namespace ConsoleApp_A
{class Program{static void Main(string[] args){int a, b;bool ju1=int.TryParse(Console.ReadLine(),out a);bool ju2 = int.TryParse(Console.ReadLine(), out b);/*string s = Console.ReadLine();*/Console.WriteLine("before exchange first={0},second={1}", a, b);int c = a ^ b;a = c ^ a;b = c ^ b;Console.WriteLine("after exchange first={0},second={1}", a, b);}}
}