be ^ !be

 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.

Previous Post

Comment (1)

  1. […] debated before the usefulness of the | and & operators. The use of the | operator depends on the context in which it is […]

Leave a Reply