Two questions, four cases. Both false, one or the other true, both true. That is the whole list, and it is short enough to look at.
You already write AND and OR. Here we look at what they do to all four cases at once.
AND wants both
| A | B | A AND B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
One row out of four. AND is true in exactly one case and false in three, which is worth
knowing when you write a guard: adding an AND condition cannot add passing
cases. A redundant condition may leave the passing cases unchanged.
A real one from a game. Let the player jump if the button is down, and they are on the ground, and they are not stunned:
IF button AND onGround AND NOT stunned THEN
CALL jump
ENDThe full three-input table has eight rows. Only button true, onGround true and stunned false makes this guard true. A mistake in any required condition can prevent the jump; whether the game reaches a particular row depends on its rules.
OR wants either
| A | B | A OR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
The mirror image. OR is true in three cases and false in one. Adding an OR
condition cannot remove passing cases, but a redundant condition may add none.
Look at the last row, where both inputs are 1. A OR B is true when both are true, not just when one is.
The everyday “or” often means one or the other but not both. Tea or coffee. This one does
not. If you want that, you want the next one.
XOR wants exactly one
| A | B | A OR B | A XOR B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 0 |
XOR is exclusive or: true when exactly one side is true, false when both are.
The useful way to think of it is not “or” at all. XOR asks whether the two answers
differ. Same, and it is false. Different, and it is true.
That reading is the one to keep. It is why XOR turns up wherever something has to notice a
change, and it is the reason it will build you an adder in Gates and the Adder.
Must A AND A have fewer passing cases than A? Must A OR A have more?
No to both. With the same unchanged Boolean value, A AND A and A OR A
each equal A: false when A is 0, true when A is 1. Repeating the condition
neither removes nor adds a passing case.
Reading a column instead of a row
Look at the last table again, at the two columns rather than the rows.
OR and XOR are identical in three of four cases. If you tested a program by trying the
first three, you would find no difference at all. The bug would be waiting in the fourth.
That is what the table is for. It shows you the case you would not have thought to try.
When it’s wrong, see why
- The test never passes. Check each required
ANDcondition. A contradictory condition can rule out every case; repeating a condition need not narrow anything. - The test always passes. Look for an always-true
ORcondition. Repeated conditions need not widen the result, but an always-true one accepts every row. - It worked until both things happened at once. That is the
ORandXORrow. Decide which you meant. - You cannot see the bug by reading the line. Write the table. Four rows takes a minute, and it will show you the case you skipped.
What you’ve learnt
- Two questions make four cases, and three make eight.
ANDis true only when both Boolean inputs are true; an added condition cannot add passing cases.ORincludes the both-true case; an added condition cannot remove passing cases.XORasks whether the two answers differ.- Comparing two columns shows you which case tells them apart.
What’s next
You can read a test now. In Unit 3 we use that to prove two different-looking tests are the same test, and swap the tangled one for the plain one.