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 a ∧ b 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) (a ∧ b 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) (a ∨ b 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) a ∨ b;
(ii) NOT(a ∨ b);
(iii) a ∧ (a ∨ b).
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 true ∨ false = true.
(ii) We should evaluate the term in brackets first: true ∨ false = true. Then NOT(true) = false. So NOT(a ∨ b) = false.
(iii) Again, evaluate the term in brackets first: (true ∨ false) = true. Then the given expression becomes true ∧ true which evaluates to true.