Skip to content

Both, Either, Different

Two questions make four cases. AND, OR and XOR are three of the ways of answering them.

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

ABA AND B
000
010
100
111
One row true out of four. AND is a demanding word.

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: every AND you add makes a test harder to satisfy.

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
END

Three questions is eight cases, and exactly one of them jumps. That is the intent, and it is also the risk: with eight cases and one success, a mistake in any condition shows up as a player who cannot jump and no obvious reason why.

OR wants either

ABA OR B
000
011
101
111
Three rows true out of four. OR is a generous word.

The mirror image. OR is true in three cases and false in one, so every OR you add makes a test easier to satisfy.

Note the top row of the pair. 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

ABA OR BA XOR B
0000
0111
1011
1110
The two agree everywhere except the bottom row, where both inputs are true.

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.

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. Count your ANDs. Each one narrows it, and one impossible condition among them makes the whole thing impossible.
  • The test always passes. Count your ORs. Each one widens it, and one always-true condition among them makes the whole thing always true.
  • It worked until both things happened at once. That is the OR and XOR row. 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.
  • AND is true in one case of four, so it makes a test harder to satisfy.
  • OR is true in three of four, so it makes a test easier.
  • XOR asks 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.