tags:

views:

122

answers:

4

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 ugly.

+3  A: 

Ok to add some text: You can look here Tables

There you can see that "exclusive or" is basicly nothing else then "not equal". So you could use just this (with boolean):

if( X != Y )...

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

InsertNickHere
+13  A: 
bool xorValue = bool1 ^ bool2;
Ronald Wildenberg
+7  A: 

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

bool result = x^y //x XOR y
this. __curious_geek
A: 

I think it should help:

A ^ B ? TrueOperation() : FalseOperation();
Jalal Amini