2024-05-29 09:41:03  1237 0

C#中异或运算符 ^

 标签:   

"^"为C#当中的异或运算符,通常可以理解为"排他性"运算.

运算规则如下


--------------------------------------------------------------------------------------------------


True ^ False                               True


True ^ True                                 False


False ^ True                                True


False    ^    False                             False

案例:

Console.WriteLine("请输入一个整型数字");
int myInt = Convert.ToInt32(Console.ReadLine());
bool isLessThan10 = myInt < 10;
bool isBetween0And5 = (0 <= myInt && myInt <= 5);
Console.WriteLine($"Integer less than 10 ? {isLessThan10}");
Console.WriteLine($"Integer between 0 and 5{isBetween0And5}");
Console.WriteLine($"Exactly one of the above is true?{isLessThan10 ^ isBetween0And5}");

结果:

image.png

可以看到 

isLessThan10 = true 
isBetween0And5  = true
根据上面理论知识
true ^ true = false
因此结果是 false