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.
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.
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:
- List the inputs in the first columns.
- 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.
- 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.
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.
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.
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.
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.
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.
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.
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:
- Find the statements: the parts that are true or false. Give each a short name.
- Find the joining words, and turn each into AND, OR or NOT.
- Put brackets round any part that must be worked out first.
- 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.)
“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.
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.
Logic in search
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.
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
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.
Write the truth tables of AND, OR and NOT, and say in one sentence what each one does.
Answer
Draw the symbols of the AND, OR and NOT gates, and label the inputs and the output of each.
Answer
Copy and complete the truth table.
| A | B | NOT A | NOT A OR B |
|---|---|---|---|
| 0 | 0 | ||
| 0 | 1 | ||
| 1 | 0 | ||
| 1 | 1 |
Answer
Explain the difference between OR in everyday speech and OR in logic. Give an example.
Answer
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
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
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
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
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
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
Quiz yourself: Data and representation, Grades 6–12 →
Practice every question in the book, and keep track of the ones you miss →
