#1,110 – The Bitwise Exclusive OR Operator

You can use the ^ operator to do a bitwise exclusive OR (“XOR”) operation between two integer-based values.

An exclusive OR operation is applied to two different bits, returning true if exactly one of the input bits is true.  Here is a truth table showing the output value for all possible input combinations.

  • 0 ^ 0 = 0
  • 0 ^ 1 = 1
  • 1 ^ 0 = 1
  • 1 ^ 1 = 0

You can use the ^ operator on two arbitrary integer values as shown below.  The operation is applied to the two integer values, one bit at a time.

            int n1 = 12;
            int n2 = 10;
            int result = n1 ^ n2;
            Console.WriteLine("{0} ^ {1} = {2}", n1, n2, result);

1110-001
It may help to use hex notation in order to better understand how the exclusive OR operation works on each bit.

            int n1 = 0xC;  // 12 (dec) or 1100 (bin)
            int n2 = 0xA;  // 10 (dec) or 1010 (bin)

            // Result = 0110 bin (6 dec)
            int result = n1 ^ n2;

1110-002

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

One Response to #1,110 – The Bitwise Exclusive OR Operator

  1. Pingback: Dew Drop – June 4, 2014 (#1790) | 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: