Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Back to Basics: XOR

Conditionals are a common spot for the unintended creation of un-maintainable and un-readable code.  To mitigate complicated conditionals, refactoring with Decompose Conditional always helps in conveying what you're trying to do.  In other times, it's simply a matter of making the conditional logic itself less complicated.  An oft-overlooked operator within C#, or within any language that supports it, is mutual exclusivity, or XOR.  This is provided by the ^ character in C#.  XOR states that a conditional will return true if one operand is true but not the other.  The following demonstrates the differences between AND, OR and XOR.

AND

Operand Operand Result
False False False
False True False
True False False
True True True

OR

Operand Operand Result
False False False
False True True
True False True
True True True

XOR

Operand Operand Result
False False False
False True True
True False True
True True False

Note again that XOR returns false if both are true or both are false, but returns true when only one operand is true.

When used appropriately, XOR can greatly simplify a conditional statement and ease of readability. Consider the following conditional: ((a > 0 && b <= 0) || (b > 0 && a <= 0)). Using the XOR operator, this conditional can be simplified to: (a > 0 ^ b > 0). Clean, simple, and easy to read.  A review of other C# operators on the MSDN site may lead to the discovery of other coding simplifications that you were not aware of or had long forgotten...or more concisely:  (not aware of ^ long forgotten).  Sorry, had to do it.

Billy McCafferty


Posted 11-24-2007 8:47 AM by Billy McCafferty
Filed under:

[Advertisement]

Comments

Brian Chavez wrote re: Back to Basics: XOR
on 11-24-2007 12:50 PM

very cool Billy.  I agree, much cleaner, more maintainable, and more readable.  Great post!

Mike wrote re: Back to Basics: XOR
on 11-24-2007 1:48 PM

Good one Billy...I'll keep an eye out where I can use this to clean things up a bit...

Luis Abreu wrote re: Back to Basics: XOR
on 11-24-2007 2:00 PM

Pretty cool as always. Though I must say that I think that most guys will always prefer the 1st aproach (I prefer the 2nd too, but my experience shows that people will write the second option even though they know about xor...)

KG2V wrote re: Back to Basics: XOR
on 11-26-2007 7:16 AM

I find that most programmers don't have a clue about even basic boolean algebra!  Even the basics like Demorgan's theorem

to whit

if you have

!A and !B

it is the sane as

!(A or B)

and

!A or !B is

!(A and B)

where this comes in useful is for things like

!(!A or B)

which becomes

!!A and !B

or

A and !B

"break the bar, reverse the and/or" (in logic class a bar over the variable is the "not")

» Daily Dose of Links - 20071205 Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats. wrote &raquo; Daily Dose of Links - 20071205 Alvin Ashcraft&#8217;s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.
on 12-05-2007 10:08 AM

Pingback from  &raquo; Daily Dose of Links - 20071205 Alvin Ashcraft&#8217;s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.

Nick wrote re: Back to Basics: XOR
on 03-16-2008 3:18 AM

The way I always remember XOR is by saying it's "your  momma's OR":

"You can have cake OR pie for dessert ... but not both."

Mike wrote re: Back to Basics: XOR
on 10-05-2008 1:41 AM

well how to do a XOR if there are 3 conditions, say for instance i want a true only if one of a,b,c is = 10

i.e. ((a==10) ^ (b==10) ^ (c==10)) it does not work this way in code i have checked (i use C#)

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)