#1,106 – Using the Logical Exclusive OR Operator

You can use the exclusive OR (^) operator to do a bitwise exclusive OR operation.  The operands in this case are integer-based types.

You can also use the exclusive OR operator on boolean operands.  The result of the operation is true if exactly one of the operands (but not both) is true.

  • false ^ false => false
  • false ^ true => true
  • true ^ false => true
  • true ^ true => false
            bool itsThursday = DateTime.Now.DayOfWeek == DayOfWeek.Thursday;
            bool itsMay = DateTime.Now.Month == 5;

            // Wear red shirt in May, wear red shirt every Thursday,
            // but don't wear red shirt on Thursdays in May
            bool wearRedShirt = itsThursday ^ itsMay;

            Console.WriteLine("Thursday:{0}, May:{1}, RedShirt:{2}", itsThursday, itsMay, wearRedShirt);

1106-001

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #1,106 – Using the Logical Exclusive OR Operator

  1. Pingback: Dew Drop – May 29, 2014 (#1786) | Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: