Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 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). 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 TRUE FALSE TRUE • They might involve a variable, e.g. if x = 10: – x is equal to 1 FALSE – x is more than 5 TRUE – x is NOT equal to 5 TRUE 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 != “”:) 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: Inputs and outputs are p NOT p 0 1 1 0 labelled NOT operates on a single value, so there are only two possible inputs; 0 or 1 Truth Table - OR The OR operator gives a true result if any of the input values is true, e.g. p 0 0 1 1 q 0 1 0 1 p OR q 0 1 1 1 OR operates on two values, so there are four possible inputs Truth Table - AND The AND operator gives a true result if both of the input values are true, e.g. p 0 0 1 1 q 0 1 0 1 p AND q 0 0 0 1 Truth Table - EOR The EOR operator gives a true result if the two input values are different, e.g. p 0 0 1 1 q 0 1 0 1 p EOR q 0 1 1 0 Is Anything Always True? The following always give the same results: • p OR NOT p is always 1 p 0 1 NOT p 1 0 P OR NOT p 1 1 • p AND NOT p is always 0 p 0 1 NOT p 1 0 P AND NOT p 0 0 Combining Operators • AND and OR can work with more than two inputs, just take a pair of inputs at a time: 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 • Just like BIDMAS for arithmetic, there is a correct order to combinations of AND and OR. 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. Duality • Look at the truth tables for AND and OR – what do you see? p q p AND q p q p OR q 0 0 0 1 1 1 0 1 0 1 0 1 1 0 0 0 1 1 1 1 1 0 0 0 • 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) 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.