开发者

XOR operation for two boolean field

开发者 https://www.devze.com 2023-01-06 14:50 出处:网络
Given two Boolean, how to come up with the most elegant one liner that computes the XOR operation in C#?

Given two Boolean, how to come up with the most elegant one liner that computes the XOR operation in C#?

I know one can do this by a combination of switch or if else but that would make my code rather u开发者_C百科gly.


bool xorValue = bool1 ^ bool2;


Ok to add some context: You can look here Tables

There you can see that "exclusive or" is basically the same as "not equal". So you could just use this (with boolean):

if (X != Y)...

But if you want to directly show people you mean "XOR" just use the other answers here.


C# has logical XOR operator ^. Here's how you do it.

bool result = x ^ y // x XOR y


I think it should help:

A ^ B ? TrueOperation() : FalseOperation();
0

精彩评论

暂无评论...
验证码 换一张
取 消