← StudyX Devlog

AND, OR and NOT as Puzzles

Hiroki ObaraHiroki Obara · DeveloperMath GatesHow-To
AND, OR and NOT as Puzzles

AND, OR and NOT are the most basic operations a computer uses to make decisions. They are called logic operations. As soon as children start programming, they meet them in the form “if A and B” or “if A or B”.

The words sound technical, but what they mean is almost the same as everyday language. In this post I explain the three operations with familiar examples and turn them into puzzles you can play at home.

True and false

Logic operations do not work with numbers. They work with whether something is right or not. Something that is right is called true, and something that is not is called false.

“It is raining today” is true on a rainy day and false on a sunny one. “7 is greater than 10” is always false. Logic operations take answers like these, true or false, and combine them or turn them over.

NOT: turn it over

NOT turns true into false and false into true.

If “it is raining” is true, then “it is not raining” is false. If “it is raining” is false, then “it is not raining” is true. The word “not” in English is exactly this operation.

Two NOTs in a row bring you back to where you started. “It is not the case that it is not raining” means the same as “it is raining”. It is a little like multiplying by −1 twice, which gives back the original number.

AND: both at once

AND is true only when both conditions are true.

Take this rule: if it is raining and it is cold, wear a coat. You wear the coat only on days that are both rainy and cold. On a warm rainy day, or a cold sunny one, you do not.

Here is that rule for every combination. A table like this is called a truth table.

It is raining It is cold Wear a coat (AND)
true true true
true false false
false true false
false false false

Only one of the four rows is true. You could say AND makes a condition strict.

OR: at least one

OR is true when at least one of the two conditions is true.

Take this rule: if it is raining or it is very windy, walk instead of cycling.

It is raining It is windy Walk (OR)
true true true
true false true
false true true
false false false

This time three of the four rows are true. OR makes a condition loose.

There is one place where children often trip up: the first row, a day that is both rainy and windy. In logic, OR is true when both are true. But in everyday speech, “or” often means picking only one, as in “coffee or tea?”. I think it helps to say clearly, at least once, that logical OR means “one or the other, or both”. The operation that is true when exactly one is true has its own name, XOR, exclusive or.

Thinking with a switch circuit

A circuit with a battery, a bulb and switches makes the difference between AND and OR visible.

Put two switches one after the other on a single path (in series), and the bulb lights only when both are on. That is AND. Put the two switches on separate paths (in parallel), and turning on either one lights the bulb. That is OR. If “switch on” means true and “bulb lit” means true, you get exactly the truth tables above.

I think this comparison is worth using, because inside a computer, logic operations really are done by combinations of tiny electrical switches. It is less a metaphor than a description.

Word puzzles at home

You do not need any equipment. Words are enough.

Point around the room and ask, “Which things are red and round?” That is AND. An apple fits, but a red book or a white ball does not. Then ask, “Which things are red or round?” That is OR, and the apple, the red book and the white ball all fit.

It is also fun to let the child set the questions. Ask them to “use NOT to make a question that only one thing in this room fits”, and you get questions like “something that is not square and is on the table”. Combining conditions until only one answer is left is the same kind of thinking as writing conditions in a program.

Once that is easy, you can try something harder. Do “things that are not (red and round)” and “things that are not red, or not round” point to the same objects? They do. This is a rule called De Morgan’s law, which children usually meet later in school maths. Even without the name, I think they can feel it by checking with things in the room.

Scratch, which many schools use, has blocks called “and”, “or” and “not”. They are exactly AND, OR and NOT. Put them inside an “if” block and you can build conditions such as: if the space key is pressed and the sprite is touching the ground, jump.

When a condition does not behave as expected, I suggest writing a truth table. Write the four rows and check what you want to happen in each case. You will notice if AND and OR have been swapped, or if NOT is in the wrong place. What programming education in school aims for is in What Do Children Learn in Elementary School Programming?, and the kind of thinking puzzles build is in What Is Computational Thinking, and What Can Puzzles Teach?.

The Logic Circuit in Math Gates

Math Gates is a circuit puzzle game I make. You place gates that change a number and mirrors that turn its path 90 degrees on the empty cells of a board, then press Run. A bit carrying a number travels in a straight line. The port at the end accepts only values that meet its condition.

The last of the seven worlds, Logic Circuit, is what this post is about. When the bit passes a comparison gate such as v>n or v<n, its number turns into true or false. It then goes through NOT, AND and OR logic gates, and you succeed if it reaches the port with a value that meets the condition. Some ports take “true” as their condition. As soon as you place a piece, the bit’s path and how its value changes are drawn on the board as a dotted line, so you can follow where true and false flip before you press Run. When you clear a puzzle, you get a discovery card that gives what you just did a name and a formula.

There are 140 puzzles across seven worlds, aimed at ages 8 and up. It is free to play with ads between puzzles, and a one-time purchase removes the ads. How to play is in Getting Started with Math Gates.

I think AND, OR and NOT are not special words for computers. They are the “and”, “or” and “not” we use every day, given a precise shape.

Download on the App Store