boolean logic & truth tables in today’s lesson we will look at: a reminder about truth values...

13
Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence equivalent expressions

Upload: georgiana-powers

Post on 23-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Boolean Logic & Truth Tables

In today’s lesson we will look at:

• a reminder about truth values and NOT, AND, OR and EOR

• truth tables

• operator precedence

• equivalent expressions

Page 2: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Truth Values

When we first looked at Boolean Logic, we introduced the idea of truth values – things that we could all agree on as being true or false.

In computing, we are usually concerned with truths about circuits, e.g.

•the switch is on

•the light is on

or mathematical truths (especially is application programming).

Page 3: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Mathematical Truths• We have looked at Boolean variables, and also used if to test the truth of mathematical comparisons. Mathematical truths are usually easier to determine:

– 1 is equal to 1

– 2 is more than 5

– 3 is NOT equal to 5

• They might involve a variable, e.g. if x = 10:– x is equal to 1

– x is more than 5

– x is NOT equal to 5

TRUE

FALSE

TRUE

FALSE

TRUE

TRUE

Page 4: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Truth Values• Truth values are pairs of opposites, and are usually

written in one of three ways:– On or Off

– True or False

– 1 or 0

• For the rest of this topic we are going to use 0 and 1 as they are easier to write.

• Most programming languages also allow you to use numbers as Boolean values, with 0 as False and non-zero as True (and Python even allows you to use strings, where “” is False and a non-blank string is True), e.g. if x: is the same as if x > 0: (or if x: is the same as if x != “”:)

Page 5: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Truth Table - NOT• We can use truth tables to describe the output of

a particular Boolean logic operation.

• Truth tables describe the output for all possible inputs

• e.g. the NOT operator toggles the truth value to its opposite value:

p NOT p

0 1

1 0

NOT operates on a single value, so there are only two possible inputs; 0 or 1

Inputs and outputs are labelled

Page 6: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Truth Table - OR

The OR operator gives a true result if any of the input values is true, e.g.

p q p OR q

0 0 0

0 1 1

1 0 1

1 1 1

OR operates on two values, so there are four possible inputs

Page 7: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Truth Table - AND

The AND operator gives a true result if both of the input values are true, e.g.

p q p AND q

0 0 0

0 1 0

1 0 0

1 1 1

Page 8: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Truth Table - EOR

The EOR operator gives a true result if the two input values are different, e.g.

p q p EOR q

0 0 0

0 1 1

1 0 1

1 1 0

Page 9: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Is Anything Always True?

The following always give the same results:

• p OR NOT p is always 1

• p AND NOT p is always 0

p NOT p P AND NOT p0 1 0

1 0 0

p NOT p P OR NOT p0 1 1

1 0 1

Page 10: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Combining Operators

• AND and OR can work with more than two inputs, just take a pair of inputs at a time:

• Just like BIDMAS for arithmetic, there is a correct order to combinations of AND and OR.

p q R p OR q OR r

0 0 0 0

0 0 1 1

0 1 0 1

0 1 1 1

1 0 0 1

1 0 1 1

1 1 0 1

1 1 1 1

Page 11: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Operator Precedence

• There are rules about which operations should be done first; this is known as operator precedence:

– operations inside brackets are done first

– AND is done before OR

• In fact, sometimes a AND b is written as a.b and a OR b can be written as a+b – this can help you to remember the order, as multiplication comes before addition in BIDMAS for ordinary arithmetic.

Page 12: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

Duality

• Look at the truth tables for AND and OR – what do you see?

• If you swap 0 for 1 you get the other table!

• This is a property known as duality, and it can be useful for simplifying calculations (or logic circuits)

p q p OR q

1 1 1

1 0 1

0 1 1

0 0 0

p q p AND q

0 0 0

0 1 0

1 0 0

1 1 1

Page 13: Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence

DeMorgan’s Duals

• This property leads to two rules, called DeMorgan’s Duals:

– NOT(a OR b) = NOT a AND NOT b

– NOT(a AND b) = NOT a OR NOT b

• It’s unlikely that you will need to know these for GCSE Computing, but they can be useful for simplifying calculations or logic circuits.

• We will look at logic circuits and the symbols used in them in the next lesson.