This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Boolean Operators

The eighth part of the C# Fundamentals tutorial moves away from arithmetic and takes a first look at the Boolean data type and its available operators. Boolean data is used extensively in programming and an understanding of its features is essential.

Exclusive OR (XOR) Operator

The exclusive OR, or XOR, operator is a specialised version of OR. The XOR operator returns true if either or the operands is true but false if they are both true, ie. exactly one of the two operands must be true. This can be represented by the following truth table.

Operand 1Operand 2Result (XOR)

The caret character (^) represents the exclusive OR operator:

bool a = true;
bool b = false;
bool c = true;
bool result;

result = a ^ b;         // result = true
result = a ^ c;         // result = false
result = a ^ true;      // result = false

Short-Circuit Logic Operators

C# programming includes two further operators for use with Boolean values. These are short-circuit versions of AND and OR. They are included in code using either a double-ampersand (&&) or double-bar (||).

When evaluating an AND where the first operand evaluates to false, the result of the operation will be false regardless of the value of the second operand. Similarly, when evaluating an OR operation where the first operand evaluates to true, we know the result will always be true. The short-circuit versions of AND and OR use this logic to create more efficient code by not evaluating the second operand on these occasions. Consider the following examples:

bool a = true;
bool b = false;
bool result;

result = a && b;        // result = false
result = b && a;        // result = false, a is not evaluated
result = a || b;        // result = true, b is not evaluated
result = b || a;        // result = true

In many situations, the short-circuit and standard versions of AND and OR are equivalent. It is important to recognise that when the operation is short-circuited the second operand is not evaluated and so any other function linked to the evaluation is not carried out. The following example illustrates this:

int divisor = 0;
int quantity = 100;
bool ok;

// Check if quantity divides without reminder (divisor must not be zero)
ok = (divisor != 0) & (quantity % divisor == 0);

This program causes an error when executed. On the line of code where the variable "ok" is assigned, two operations are carried out. Firstly the divisor value is checked to ensure that it is not zero. Secondly, the quantity is divided by the divisor. As the second operand is evaluated as one hundred divided by zero, a divide by zero exception is generated and the program stops.

In the above example, it would be better to check the divisor and only perform the modulus operation if it is non-zero. This can be achieved by using the short-circuit version of AND.

int divisor = 0;
int quantity = 100;
bool ok;

// Check if quantity divides without reminder (divisor must not be zero)
ok = (divisor != 0) && (quantity % divisor == 0);

The above example shows the use of the short-circuit operators to avoid errors. It is important to understand that there can be drawbacks to not evaluating the second operand. If, for example, an increment operator (++) is used in the second operand, not evaluating this would mean that a variable's value would not be adjusted. Inappropriate use such as this can lead to bugs that are difficult to identify and resolve.

Operator Precedence

We can now extend the operator precedence table, adding the above operators.

Parentheses Operator
Increment / Decrement Operators
++(postfix) --(postfix) ++(prefix) --(prefix)
Complement Operators
Basic Arithmetic Operators
* / % + -
Equivalence Operators
== !=
Logic Operators
& ^ | && ||
Assignment Operator
Compound Assignment Operators
*= /= %= += -=
28 August 2006