Skip to content

← Data and representation

True or False: Logic

Unit 4 · Data and representation

Hide the stories, facts, photos, activities and interactives, for revising before an exam
In this unit 13 sections
  1. True or false
  2. AND: both must be true
  3. OR: at least one is true
  4. NOT: the opposite
  5. Truth tables
  6. Logic gates
  7. Wiring gates together
  8. Everyday rules as logic
  9. Logic in programs
  10. Logic in search
  11. Where logic came from
  12. In short
  13. Exercise

At the gate of a school function, a teacher checks each student: “Have you got your invitation card? And are you in Grade 6, 7 or 8?” Two questions, each answered yes or no, and one rule that joins them. A solar street light has a rule like that too: it switches on when it is dark and something moves near it. A rule of that kind, built from yes-or-no answers joined by small words, decides more things around you than you might think.

In Unit 3 your programs made choices with conditions that were true or false. In this unit you will join conditions together with three small words, AND, OR and NOT, and see that these same three ideas are built into the hardware of every computer, as tiny circuits called logic gates. You will fill in truth tables, wire gates to make a street light and a water pump work, write everyday rules as logic, fix programs whose logic is wrong, and use AND, OR and NOT to search.

True or false

Some sentences are either true or false. “Kathmandu is the capital of Nepal” is true. “Pokhara is in the Terai” is false: Pokhara is in the hills. A sentence like that is called a statement.

Other sentences are neither. “Close the door” is an instruction, “Is it raining?” is a question, and “Wow!” is a feeling. None of them is true or false. “Momo is the best food” is an opinion: true for some people and false for others, so a computer can’t work with it either.

A computer works with statements whose answer is exactly one of the two. In Unit 3 you met them in programs as conditions, like age < 13, and their values, true and false, as the Boolean data type. Since Grade 6 you have also known that a computer is built from switches that are on or off, and that a bit is 1 or 0. These are the same two values with different names:

In a program As a bit As a switch
true 1 on
false 0 off

Truth tables and logic circuits are usually written with 1 and 0, so this unit uses both.

Logic is the study of how true and false statements are combined. The kind of logic computers use, with just two values, is called Boolean logic. It has three basic logical operators: AND, OR and NOT. Each one takes true and false values and gives back a true or false value.

Try it

True or false, or neither?

For each sentence, decide whether it is a statement a computer could work with (true or false), or neither.

Kathmandu is the capital of Nepal.

Is it true or false, or neither?

Tap a group above.

Card
1 of 10
Right the first time
0

Which of these is a statement, with a true or false value?

Pick an answer to see why.

AND: both must be true

Here is a rule from a school notice: “You may join the science trip if your parents have signed the form and you have paid the fee.” A student who has done both may go. A student who has done only one, or neither, may not.

In logic, AND joins two statements, and the result is true only when both of them are true. If either one is false, or both are, the result is false.

There are only four possible cases, and you can show all of them in a table. Call “the form is signed” A, and “the fee is paid” B:

A (form signed) B (fee paid) A AND B (may go)
0 0 0
0 1 0
1 0 0
1 1 1

A table like this, with one row for every possible combination of inputs and the result for each, is called a truth table. The values you start with (here A and B) are the inputs; the result is the output. AND gives 1 in only one row of its four: the row where both inputs are 1.

Notice the order of the rows: 00, 01, 10, 11. That is counting in binary, 0, 1, 2, 3, as in Unit 2. Writing the rows in this order makes sure you don’t miss one or write one twice.

A solar street light with a motion sensor follows an AND rule: it comes on when it is dark AND something is near. Here is the rule in blocks. The blocks run once for each row of the truth table, and the table shows what light holds after each run.

Try it

A light that needs two things

The table above the blocks shows light for all four rows: the light comes on in only one of them. Tap the dark or near box to flip it, then press Run: the shaded row of the table is the one the boxes are on. Then tap and in the block and change it to or. What happens to the table?

What the program says and asks appears here.

light – no value
darknearlight
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

Press Run to start the program, or Step to do one block at a time.

Loading the blocks…

What you should see

A rice shop gives a free bag to a customer who buys more than 20 kg and pays in cash. Ram buys 25 kg and pays by mobile wallet. Does he get the bag?

Pick an answer to see why.

OR: at least one is true

A different notice: “Students can pay the fee in cash or by mobile wallet.” A student who pays either way is fine.

OR joins two statements, and the result is true when at least one of them is true: the first, the second, or both. It is false only when both are false.

A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1

OR gives 0 in only one row, the row where both inputs are 0. That is the opposite pattern to AND, which gives 1 in only one row.

A school canteen gives a discount to anyone who is a student or a teacher. Gita is a student who also helps teach the Grade 1 class, so she counts as both. Does she get the discount?

Pick an answer to see why.

Look at the truth tables of AND and OR. In how many of the four rows do they give the same output?

Pick an answer to see why.

NOT: the opposite

The third operator is the simplest. NOT takes one statement and turns it into its opposite: NOT true is false, and NOT false is true. Its truth table has only two rows, because it has only one input:

A NOT A
0 1
1 0

If raining is true, NOT raining is false. If “the tank is full” is false, “NOT the tank is full” is true: the tank is not full.

NOT is most useful together with AND and OR. Think of the water pump many houses in Nepal use to fill the tank on the roof. It should run when there is water to pump AND the tank is NOT full. If the second part were left out, the pump would carry on after the tank was full, and the water would overflow from the roof.

full is false. What is NOT full?

Pick an answer to see why.

Truth tables

You have now seen the truth table of each operator on its own. A truth table can be made for any combination of them, and it is the surest way to know exactly what a rule does: it lists every possible case, so nothing is left to guesswork.

To make one:

  1. List the inputs in the first columns.
  2. Write every combination of inputs, counting in binary. With 1 input there are 2 rows; with 2 inputs, 4 rows; with 3 inputs, 8. Each extra input doubles the rows: with n inputs there are 2ⁿ rows, the same as the patterns n bits can make in Unit 2.
  3. Work out the expression one step at a time, in a column for each step, from the inside out. Each new column uses only the columns to its left.

When an expression mixes operators, NOT is done first, then AND, then OR, just as × is done before + in maths. Brackets change the order, as they do in maths. So NOT A AND B means (NOT A) AND B, while NOT (A AND B) means: work out A AND B first, then turn it round.

Try it

Fill in a truth table

Fill in the column NOT B first; one cell is done for you. Then use it to fill A AND NOT B. Press Check when every cell has a 1 or a 0.

ABNOT BA AND NOT B
00
01
10
11

Tap an empty cell to write 1; tap again for 0. Fill each column from the columns to its left, then press Check.

Try it

Two ways to say the same thing

Fill in all three columns. Then look at the last two. What do you notice?

ABA AND BNOT (A AND B)NOT A OR NOT B
00
01
10
11

Tap an empty cell to write 1; tap again for 0. Fill each column from the columns to its left, then press Check.

What you should see

How many rows has a truth table with 4 inputs?

Pick an answer to see why.

Logic gates

So far AND, OR and NOT have been ideas. Inside a computer they are made of electricity.

Look at the two circuits in Figure 4.1. Each has a battery, a bulb and two switches, A and B. A closed switch lets the current through (1); an open switch stops it (0); the bulb is 1 when it is lit.

AND: switches in seriesbatteryA closedB openbulb offOR: switches in parallelbatteryA closedB openbulb on AND: switches in seriesbatteryA closedB openbulb offOR: switches in parallelbatteryA closedB openbulb on
Figure 4.1 Two switches can make AND or OR. In series, the current must pass through A and then B, so the bulb lights only when A AND B are closed. In parallel, side by side, the current can go through either one, so A OR B is enough. Here A is closed and B is open in both.

A computer does the same with transistors, the tiny switches you met in Grade 6, which are switched by electricity instead of by a finger. A few transistors joined together make a logic gate: a tiny circuit that takes inputs (usually two; a NOT gate has one), each 1 or 0 (a wire with a voltage on it, or without), and gives one output by the rule of AND, OR or NOT. There is an AND gate, an OR gate and a NOT gate (a NOT gate is also called an inverter).

Each gate has its own symbol, used in circuit diagrams all over the world, and in exams.

AND1 only when both inputs are 1ABQABQ000010100111OR1 when at least one input is 1ABQABQ000011101111NOTthe opposite of its inputAQAQ0110 AND1 only when both inputs are 1ABQABQ000010100111OR1 when at least one input is 1ABQABQ000011101111NOTthe opposite of its inputAQAQ0110
Figure 4.2 The symbols of the AND, OR and NOT gates, each with its inputs A and B on the left, its output Q on the right, and its truth table. To draw them: AND is a D, flat at the back and round at the front; OR is like a shield, with a curved back and a pointed front; NOT is a triangle with a small circle at its point. The small circle is what means “turn it round”.

Try it

One gate, three kinds

Tap the switches A and B to try all four rows. Each row you try fills in the table: the output Q is the lamp. Then tap the gate to change it to OR, and then to NOT, and try every row again. (A NOT gate has only one input, so B does nothing.) The thick, colored wires carry 1; the thin gray ones carry 0.

A0B0ANDchange0Q
ABQ
00
01
10
11

Tap the switches, or a row of the table, to try every row. Each row you try fills in.

A small black rectangular chip with two rows of seven metal legs, and SN74LS08N written on its top
Figure 4.3 A chip with four AND gates inside it. The legs carry the inputs and outputs of each gate, and the power that runs them. Chips of this family have been made since the 1960s, and are still used in school and hobby electronics. Photo: RetroEditor, CC BY 4.0

Two switches are joined one after the other (in series) with a bulb. When does the bulb light?

Pick an answer to see why.

Which symbol has a small circle at its point?

Pick an answer to see why.

Wiring gates together

One gate on its own can only do AND, OR or NOT. Join them, with the output of one gate wired to an input of another, and you can make any rule at all. Gates wired together like this make a logic circuit.

Start with the street light from earlier: the light comes on when it is dark AND someone is near. In the widget below the sensors are switches, and the lamp is the light. Its goal column shows what the light should do in each row.

Try it

Wire the street light

Wire the circuit so that every row of the table matches the goal. To join two dots, tap the dot on the right of a switch or gate, then the dot on the left of a gate or the lamp. Tap a wired dot on the left of a gate again to take its wire away.

dark0near0AND?light
darknearlightgoal
00?0
01?0
10?0
11?1

Tap a dot on the right of a switch or gate, then a dot on the left of a gate or the lamp, to join them.

Answer

Now the water pump. It must run when there is water in the well AND the tank is NOT full. That needs two gates: a NOT, and an AND.

Try it

Wire the water pump

Wire the two gates so that the pump matches its goal in every row.

water0full0NOTAND?pump
waterfullpumpgoal
00?0
01?0
10?1
11?0

Tap a dot on the right of a switch or gate, then a dot on the left of a gate or the lamp, to join them.

Hint
Answer

The inverter light

In the years of load-shedding, many homes in Nepal had an inverter: a box that charges a battery while the mains electricity is on, and powers some lights from the battery when it goes off. A light on an inverter shines when its switch is on AND there is power from the mains OR the battery:

switch AND (mains OR battery)

The brackets matter. Without them, AND would be done first, and the rule would mean (switch AND mains) OR battery: the light would come on from the battery even with its switch off.

Try it

Wire the inverter light

This rule has three inputs, so the table has 8 rows. Both gates start as AND gates: tap a gate to change its kind. Wire them so that the bulb matches the goal in every row.

switch0mains0battery0ANDchangeANDchange?bulb
switchmainsbatterybulbgoal
000?0
001?0
010?0
011?0
100?0
101?1
110?1
111?1

Tap a dot on the right of a switch or gate, then a dot on the left of a gate or the lamp, to join them.

Hint
Answer

Try it

The inverter light on paper

Here is the same rule as a truth table, with S for switch, M for mains and B for battery. Work out M OR B first, then S AND (M OR B). The first two cells are done for you.

SMBM OR BS AND (M OR B)
000
001
010
011
100
101
110
111

Tap an empty cell to write 1; tap again for 0. Fill each column from the columns to its left, then press Check.

In the inverter light, the switch is on, the mains is off and the battery is charged. Is the light on?

Pick an answer to see why.

Try it

Free play: four gates

Build any circuit you like with three switches and four gates; tap a gate to change its kind. Besides AND, OR and NOT there are three more kinds to try: XOR (one or the other, but not both), NAND (AND, then NOT) and NOR (OR, then NOT). The table shows what your circuit does for all 8 rows. Can you make the lamp 1 only when A and B are on and C is off? And then only when none of the three is on? Your wires are kept in this browser.

A0B0C0ANDchangeORchangeNOTchangeANDchange?Q
ABCQ
000?
001?
010?
011?
100?
101?
110?
111?

Tap a dot on the right of a switch or gate, then a dot on the left of a gate or the lamp, to join them.

Answer

Everyday rules as logic

Rules in notices, games and machines are often written in words. To make a computer follow one, or to build it from gates, you first write it as logic. Here is how:

  1. Find the statements: the parts that are true or false. Give each a short name.
  2. Find the joining words, and turn each into AND, OR or NOT.
  3. Put brackets round any part that must be worked out first.
  4. Check it with a truth table: does every row do what the rule means?

The joining words aren’t always “and”, “or” and “not”:

The rule says In logic Example
“both … and …”, “but”, “as well as” AND “cheap but good” is cheap AND good
“either … or …”, “at least one of” OR “either a student or a teacher”
“not”, “no”, “isn’t” NOT “not full”
“unless” AND NOT “the bell rings at 10 unless it is a holiday” is ten AND NOT holiday
“neither … nor …” NOT ( … OR … ) “neither cold nor wet” is NOT (cold OR wet)

“Unless” brings in an exception: the rule works except when the second part is true. In everyday talk that means the exception stops it, so in a rule for a machine or a program we write AND NOT. (Strictly, “A unless B” only says what happens when B is false, so logic books sometimes write it A OR B; you will meet that in later grades.)

Try it

The rice cooker’s warm light

An electric rice cooker has two lights. Its warm light is on when the cooker is plugged in AND NOT cooking. Build that rule in the set warm to block, in place of false, using the and and not blocks and the variables. The table checks your rule in all four rows.

What the program says and asks appears here.

warm – no value
pluggedcookingwarmshould be
falsefalsefalsefalse✓
falsetruefalsefalse✓
truefalsefalsetrue✗
truetruefalsefalse✓

The ✗ rows of the table don’t match the rule yet. The table changes as soon as you change the blocks.

Loading the blocks…

Hint
Answer

“The alarm rings at 6 o’clock unless it is a holiday.” Which is this rule?

Pick an answer to see why.

Logic in programs

In Unit 3 an if block checked one condition. With and, or and not blocks, one if can check several at once.

A trip is for Grades 6 to 8. The condition “the grade is 6, 7 or 8” is two conditions joined: grade > 5 and grade < 9. Here is a program for the trip, but its author used the wrong word.

Try it

The trip that let everyone in

Run it as a Grade 7 student and as a Grade 10 student. Then press Test. Which tests fail, and why? Fix the program.

What the program says and asks appears here.

grade – no value
TestShould sayIt said
Grade 7You can come on the trip!
Grade 6You can come on the trip!
Grade 8You can come on the trip!
Grade 5Sorry, this trip is for Grades 6 to 8.
Grade 9Sorry, this trip is for Grades 6 to 8.

Press Run to start the program, or Step to do one block at a time.

Loading the blocks…

Hint
Answer

Try it

Free on Saturdays

A museum (a made-up one) is free on Saturdays. It also wants to let children under 5 in free on every day. Press Test: one test fails. Add the new rule to the condition with an or block, and test again.

What the program says and asks appears here.

day – no value
age – no value
TestShould sayIt said
Saturday, age 30Free entry today!
Monday, age 3Free entry today!
Monday, age 12Entry: Rs 50
Friday, age 5Entry: Rs 50

Press Run to start the program, or Step to do one block at a time.

Loading the blocks…

Hint
Answer

NOT is useful in programs too. In Unit 3’s guessing game, the loop asked if found = false. The same can be written if not found: “if we haven’t found it yet”.

age is 15 and ticket is false. What is age > 12 and ticket?

Pick an answer to see why.

AND, OR and NOT are also how you tell a search what you want. Each web page either has a word or doesn’t: true or false. A search for two words joined by AND finds the pages that have both; OR finds the pages that have either; NOT leaves out the pages with a word.

The widget shows twelve made-up pages about wildlife in Nepal. The two circles hold the pages with the word rhino and the pages with the word Chitwan; the pages with both are where the circles overlap. A picture like this, of groups as overlapping circles, is called a Venn diagram, after John Venn, an English mathematician who drew them in 1880.

Try it

Search with AND, OR and NOT

Try each search, and watch which part of the picture lights up and how many pages it finds.

rhino AND Chitwan
rhinoChitwan123456789101112
One word:
Two words:
Pages found
3 of 12
  1. Counting rhinos in Chitwan
  2. A rhino calf is born in Chitwan
  3. Rhinos by the Rapti River in Chitwan
  4. Rhinos moved to Bardiya
  5. The one-horned rhino: facts
  6. Rhinos of Shuklaphanta
  7. Tharu villages near Chitwan
  8. Bird watching in Chitwan
  9. Going to Chitwan by bus
  10. Red pandas of Ilam
  11. Tigers of Bardiya
  12. Snow leopards of Dolpa

AND finds only the pages with both words: where the two circles overlap. AND makes a search narrower.

So:

  • AND narrows a search: fewer pages, each with all your words. Use it when a search finds too much.
  • OR widens it: more pages, with any of the words. Use it for words that mean the same thing: momo OR dumpling.
  • NOT cuts out what you don’t want: jaguar NOT car finds the animal, not the car.

The filters in a shopping app often work the same way. Tick two brands, and you see phones of either brand: OR. Tick a brand and a price range, and you see only phones that are both: AND.

A search for rhino finds 6 pages, and a search for Chitwan finds 6. How many pages can rhino OR Chitwan find at most?

Pick an answer to see why.

Your search for momo recipe finds thousands of pages, many about buff momo. You want only vegetable momo. Which search helps?

Pick an answer to see why.

Where logic came from

A colored engraving of a man with dark hair and side whiskers, in a dark coat, a high white collar and a bow tie
Figure 4.4 George Boole (1815–1864), whose name is in every Boolean variable. Photo: Unknown author, Public domain
An old photograph of an older woman with a white lace collar, sitting at a desk with a pen in her hand
Figure 4.5 Mary Everest Boole (1832–1916), mathematician, teacher and writer. Photo: Unknown author, Public domain

Try it

Free play: every block

Build your own program here, with and, or and not among the blocks. Your blocks are kept in this browser.

What the program says and asks appears here.

age – no value

Press Run to start the program, or Step to do one block at a time.

Loading the blocks…

In short

Exercise

What is 1 AND 0?

Pick an answer to see why.

What is NOT (0 OR 0)?

Pick an answer to see why.

Which gate gives an output of 1 only when both its inputs are 1?

Pick an answer to see why.

How many rows has the truth table of (A OR B) AND NOT C?

Pick an answer to see why.

Which search finds the most pages?

Pick an answer to see why.

a.

Write the truth tables of AND, OR and NOT, and say in one sentence what each one does.

Answer
b.

Draw the symbols of the AND, OR and NOT gates, and label the inputs and the output of each.

Answer
c.

Copy and complete the truth table.

A B NOT A NOT A OR B
0 0
0 1
1 0
1 1
Answer
d.

Explain the difference between OR in everyday speech and OR in logic. Give an example.

Answer
e.

Two switches and a bulb are joined to a battery. Draw how the switches must be joined so that the bulb lights i. only when both switches are on, ii. when either switch is on. Which logical operator does each circuit make?

Answer
f.

Write each rule as logic, then say which output it gives for the inputs shown.

  • i.“The fan turns on when it is hot and someone is in the room.” hot = 1, someone = 0.
  • ii.“You may borrow a book if you have a library card or a teacher’s note.” card = 0, note = 1.
  • iii.“Go out to play unless it is raining.” play time = 1, raining = 1.
Answer
g.

A school’s computer lab door unlocks when a teacher’s card is shown, OR when a student’s card is shown AND it is lab time. Write the rule as logic with brackets, and draw its logic circuit with gates.

Hint
Answer
h.

A program should say “Teenager” for ages 13 to 19. Sanjay writes if age > 12 or age < 20 then say “Teenager”. What is wrong? Give an age that shows the bug, and fix it.

Answer
i.

You search for cricket and find too many pages about the insect. Write a better search, and explain how AND, OR and NOT change the number of pages a search finds.

Answer
j.

Why are computers built from logic gates? Explain how a gate is related to a switch, a bit, and the true and false values in a program.

Answer