#1,201 – Writing Conditional Logical Operators as Conditionals

The conditional logical operators (&&, ||) can short-circuit evaluation, based on the value of the left side of the expression.

  • For &&, evaluation short-circuits if left-side is false (result is false)
  • For ||, evaluation short-circuits if left-side is true (result is true)

You can also think of the conditional logical operators as being equivalent to the ternary conditional operator.

            bool leftSide = true;
            bool rightSide = false;

            // Short-circuits (right side not evaluated)
            bool bResult = leftSide || rightSide;

            // || Equivalent to
            bResult = leftSide ? true : rightSide;

            // Short-circuits if left side is false
            bResult = leftSide && rightSide;

            // && Equivalent to
            bResult = leftSide ? rightSide : false;
Advertisements

#1,200 – Logical Operators vs. Conditional Logical Operators

You can use either the logical operators (|, &) or the conditional logical operators (||, &&) when comparing two boolean values (or expressions).

            bool isFromMN = true;
            bool likesConfrontation = false;

            bool bResult = likesConfrontation & isFromMN;  // false
            bResult = likesConfrontation && isFromMN;      // false
            bResult = likesConfrontation | isFromMN;       // true
            bResult = likesConfrontation || isFromMN;       // true

The difference between these operators, when used with boolean values, is that the conditional logical operators can short-circuit evaluation, avoiding evaluation of the right side of the expression if possible.

            int val = 0;

            // Short-circuits (right side not evaluated)
            bResult = isFromMN || ((5 / val) == 1);     // true 

            // Throws exception (does not short-circuit)
            bResult = isFromMN | ((5 / val) == 1);

#1,114 – Don’t Use Shift Operators to Do Multiplication

When you use the shift operators on integer-based values, you effectively multiply a number by two when you do a left shift by one bit and divide a number by two when you do a right shift by one bit.

In years past, you’d sometimes hear a recommendation that you should prefer using the shift operators over multiplicative operators because the shift operators were faster.

With modern optimizing compilers, you should no longer worry about this level of optimization and performance.  If you need to multiply a number by a power of two, you should express your intent by using the multiplicative operators.  (E.g. “* 2” or “* 4”).  The compiler will figure out that fastest way to accomplish this.

#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

#1,109 – The Bitwise NOT Operator

You can use the ~ operator to do a bitwise NOT operation on an integer-based value.

A NOT operation is applied to a single input bit, returning true if the input bit is false, or false if it is true.  The NOT operator “flips” or “negates” the input bit.  Here is a truth table showing the output value for the possible input values.

  • ~0  = 1
  • ~1 = 0

You can use the ~ operator on an arbitrary integer value as shown below.  The NOT operation will be applied to the integer value, one bit at a time.

            int n = 5;
            int result = ~n;
            Console.WriteLine("~{0} = {1}", n, result);

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

            int n = 0x00000005;
            int result = ~n;
            Console.WriteLine(  "~{0:X8} = {1:X8}", n, result);

1109-002

1109-003

#1,108 – The Bitwise OR Operator

You can use the | operator to do a bitwise OR operation between two integer-based values.

An OR operation is applied to two different bits, returning true if either one or both input bits are 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 = 1

You can use the | operator on two arbitrary integer values as shown below.  The OR operation will be 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);

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

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

            // result = 1110
            int result = n1 | n2;

1108-002

#1,107 – The Bitwise AND Operator

You can use the & operator to do a bitwise AND operation between two integer-based values.

An AND operation is applied to two different bits, returning true if both input bits are true.  Here is a truth table showing the output value for all possible input combinations.

  • 0 & 0 = 0
  • 0 & 1 = 0
  • 1 & 0 = 0
  • 1 & 1 = 1

You can use the & operator on two arbitrary integer values as shown below.  The AND operation will be 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);

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

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

            // result = 1000
            int result = n1 & n2;

1107-002