Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XOR operation for two boolean field

Tags:

c#

xor

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.

like image 527
Graviton Avatar asked Jul 06 '10 07:07

Graviton


People also ask

What is XOR Boolean operation?

The XOR logical operation, exclusive or, takes two boolean operands and returns true if, and only if, the operands are different. Conversely, it returns false if the two operands have the same value.

How do you write Boolean XOR?

XOR Boolean Expression It can also be written as: (A + B) (A + B)

How do you do XOR operations?

To find XOR of more than two numbers, represent all numbers in binary representation, add 0's before if necessary. Write them like this. and so on. To find each bit of XOR just calculate number of 1's in the corresponding bits.

What is XOR example?

Examples: 1 XOR 1 = 0. 1 XOR 0 = 1. 0 XOR 1 = 1.


2 Answers

bool xorValue = bool1 ^ bool2; 
like image 119
Ronald Wildenberg Avatar answered Sep 29 '22 12:09

Ronald Wildenberg


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.

like image 38
InsertNickHere Avatar answered Sep 29 '22 13:09

InsertNickHere