Table of Contents#### Download Safari Books Online apps: Apple iOS | Android | BlackBerry

### 3.2. Boolean Operators

#### 3.2.1. The OR Operator

#### 3.2.2. The AND Operator

#### 3.2.3. Boolean Expressions

Entire Site

Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

The NOT gate discussed in the last section is an example of a logic gate. Logic gates are circuits whose behavior is modeled on Boolean operators.

The most basic Boolean operators are AND, OR, XOR, and NOT. In the C language, we have the symbols && for AND, || for OR, and ! for NOT. The workings of these operators can be illustrated with two examples from C. In these examples,

p | q | (p OR q) |
---|---|---|

1 | 1 | 1 |

1 | 0 | 1 |

0 | 1 | 1 |

0 | 0 | 0 |

the C language statements are evaluated using a step-by-step simplification process.

The first example is an `if` statement involving the Boolean OR operator. In what follows, we suppose that `x` has already been given the value 7.

if ((x > 0) || (x < 5)) y = 10;

First the truth values of the operands may be determined. In this case, because

`(x > 0)`is true, it has the value 1; because`(x < 5)`is false, it has the value 0. These evaluations result in the following simplification of the C language statement.if (1 || 0) y = 10;

The Boolean OR operator may be applied next. The only thing OR sees is the truth values resulting from the operand evaluations. OR is given a 1 and a 0, from which it produces a 1.

if( 1 ) y = 10;

The

`if`can go to work next. All it sees in the parentheses is a 1. Hence it produces the simple assignment statementy = 10;

The end result of the process is that y is set to the value 10. Notice that the OR operator never saw the value of `x.` It needs only 0s and 1s to do its job. Consequently, the workings of the OR operator can be described in a truth table just like the truth table for the NOT gate. Table 3.2 shows the truth table for the OR operator. In truth tables for Boolean operators, the letters p and q are often used as variables for the truth values 0 and 1. Variables which have only these two possible values are sometimes called Boolean variables. Notice that the decision made in the C example could have been inferred from the second line of Table 3.2.

p | q | (p AND q) |
---|---|---|

1 | 1 | 1 |

1 | 0 | 0 |

0 | 1 | 0 |

0 | 0 | 0 |

The AND operator can also be described with a truth table. See Table 3.3. The next example shows the use of this table in evaluating a C language statement containing an AND operator.

Again suppose that

`x = 7.`if((x > 0) && (x < 5)) z = 20;

Again the operands have the values 1 and 0.

if(l && 0) z = 20;

The operation of the AND operator can be referred to the second line of Table 3.3, from which we see that a 0 results.

if( 0 ) z = 20;

The 0 in the parentheses results in a null statement. Nothing happens.

;

From these examples, it is clear that the job of the Boolean operators is to operate on truth values and produce a truth value.

Just as operators in C may be nested as in the statement

if(((x > 0) && (x < 5)) || (x = 7)) w = 30;

so Boolean operators by themselves may be nested

The resulting expressions are called Boolean expressions. Just as Boolean operators have truth tables, so do Boolean expressions. The truth table for the expression just shown is given in Table 3.4.

p | q | r | ((p AND q) OR r) |
---|---|---|---|

1 | 1 | 1 | 1 |

1 | 1 | 0 | 1 |

1 | 0 | 1 | 1 |

1 | 0 | 0 | 0 |

0 | 1 | 1 | 1 |

0 | 1 | 0 | 0 |

0 | 0 | 1 | 1 |

0 | 0 | 0 | 0 |