Data and processes in computing

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

# 5.2 Operations on Boolean values

The idea of a binary operation, and the use of infix notation, is not confined to numbers. Infix notation may be used for any process with two inputs from the same set. We look now at two binary operations on Boolean values that are often used.

The first of these takes two Boolean values, and returns true if both of the input values are true and returns false otherwise. It is a binary operation, and we shall write it using the infix notation ∧, where the symbol ∧ can be read as “and”. So ab is true only when a and b are both true. We can describe the function corresponding to this operation as below (The operation ∧ is also known as conjunction).

function (infix) (ab on Bool) return in Bool

pre   true.

post   The returned value is true if both a = true and b = true, and is false otherwise.

Alternatively, we could give the semantics of ∧ by listing the returned value in each of four cases, giving the four possible combinations of input values. Using this approach, we could express the postcondition as follows.

post The returned value is c, where

• if a = true and b = true then c = true

• if a = true and b = false then c = false

• if a = false and b = true then c = false

• if a = false and b = false then c = false.

A second binary operation on Boolean values returns true if either (or both) of the two Boolean values is true. We write this operation using the infix symbol ∨ , where ∧ can be read as “or”. The corresponding function is described below. The operation ∨ is also known as disjunction.

function (infix) (ab on Bool) return in Bool

pre   true.

post   The returned value is false if both a = false and b = false, and is true otherwise.

## Activity 25

Give the semantics of ∨ by listing the returned value in each of four cases, giving the four possible combinations of input values.

### Discussion

We can express the postcondition in the description above as follows.

post The returned value is c, where

• if a = true and b = true then c = true

• if a = true and b = false then c = true

• if a = false and b = true then c = true

• if a = false and b = false then c = false.

Another useful process on Boolean values inputs a single Boolean value, and returns the reverse of that value. This process, called negation, is not a binary operation, since it has one input only. We call this function NOT, and we have

NOT(false) = true, and NOT(true) = false.

## Activity 26

• (a) Give a full description of the function NOT.

• (b) If a = true and b = false, evaluate each of:

• (i) ab;

• (ii) NOT(ab);

• (iii) a ∧ (ab).

### Discussion

(a) A description is given below.

function NOT(a in Bool) return in Bool

pre   true.

post   The returned value is false if a = true and is true if a = false.

(b) (i) Substituting for a and b, we have truefalse = true.

(ii) We should evaluate the term in brackets first: truefalse = true. Then NOT(true) = false. So NOT(ab) = false.

(iii) Again, evaluate the term in brackets first: (truefalse) = true. Then the given expression becomes truetrue which evaluates to true.