Unlike the & and | logical operators which seem to have dubious use the
^ (XOR, exclusive or) logical operator can make things quite a bit easier.

For instance ever write a IComparable?

rather then:

if (o1 == null && o2 != null)
    return false;
if (o1 != null && o2 == null)
   return false;

you can just do:

if(o1 == null ^ o2 == null)
   return false;

like the other logical operators both sides of the conditional must be
evaluated but since we’re doing exclusive or-ing here it makes sense to
do it rather then in the cases of the other logical operators where fail
fast (or short-circuiting) conditional operators (like && and ||) would
work better.

Leave a Reply

Your email address will not be published. Required fields are marked *


928 East Plymouth Drive Asbury Park, NJ 07712