This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law. .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.

## A Review of the Boolean Data Type

In the third part of the C# Fundamentals tutorial I introduced the Boolean data type. A Boolean variable is capable of storing just two values, true or false, also known as truth values. The following examples show the simple assignment of Boolean variables, which are declared using the bool keyword.

```bool grassIsGreen = true;
bool cowsGoBaa = false;```

## Boolean Logic Operators

The Boolean data type has its own set of logical operators. These allow you to test or adjust the value of a Boolean variable. The resultant values may be used in conditional statements to determine the flow through the code. Conditional programming will be examined later in the tutorial. For this article, the next sections describe the various available operators.

### Equivalence Operator

The equivalence, or equality, operator is a binary operator, in that it operates on two values or operands. The equivalence operator compares the two operands and returns a Boolean value indicating if they match exactly. The operator symbol for equivalence is a double-equals signs (==).

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

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

### Inequality Operator

The inequality operator compares two operands and returns true if the two values are different. The operator provides the opposite functionality to the equivalence operator. The operator's symbol is an exclamation mark and an equals signs (!=). It is read as "not equal to".

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

result = a != b;        // result = true
result = b != c;        // result = false
result = b != true;     // result = true```

### NOT Operator

The NOT operator is a unary operator, as it operates on a single operand. The NOT operator inverts the value of a Boolean value. If the original value is true then the returned value is false; if the original value is false, the return value is true. The NOT operation is often known as the binary complement.

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

result = !a;            // result = false
result = !b;            // result = true
result = !true;         // result = false```

### AND Operator

The AND operator is used to compare two Boolean values. It returns true if both of the operands are true. This can be represented by the following truth table, which shows the two operands and the result of every possible AND operation.

Operand 1Operand 2Result (AND)
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

The AND operator is represented by the ampersand character (&):

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

result = a & b;         // result = false
result = a & c;         // result = true
result = a & (a == c);  // result = true```

### OR Operator

The OR operator is similar to AND, as it is used to compare two Boolean values. The OR operator returns true if either of the operands are true. This can be represented by the following truth table.

Operand 1Operand 2Result (OR)
falsefalsefalse
falsetruetrue
truefalsetrue
truetruetrue

The OR operator is represented by the bar character (|):

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

result = a | b;         // result = true
result = b | c;         // result = false
result = a | b | c;     // result = true```
28 August 2006