Unit 2: Gates
Both, either, different, neither.
Unit 2 of 8 in Digital logic for kids. Its 5 lessons are Both, Either, Different, Neither and The Gate Family — below is everything each one explains, and a question or two from it to try.
Every chip on this page was wired up and run on the gate-level simulator before it shipped, and prints exactly the table shown.
This unit is free for ever, because the first two units of every track are. Try it in the app.
✋ Both
And asks: are both of them on?
And gives 1 only when a *and* b are both 1.
Look at that next to Nand. Nand gives 0 exactly where And gives 1, and 1 everywhere else. Nand is And, upside down.
So to build And: do the Nand, then flip the answer back the right way up with a Not.
HDL
CHIP And {
IN a, b;
OUT out;
PARTS:
Nand(a=a, b=b, out=n);
Not(in=n, out=out);
}
TABLE And;
It prints
a b | out 0 0 | 0 0 1 | 0 1 0 | 0 1 1 | 1
You do not need Not either
You already know how to make a Not out of a Nand: send the same wire into both inputs.
So And really is just two Nands. The second one is a Not in disguise.
HDL
CHIP And {
IN a, b;
OUT out;
PARTS:
Nand(a=a, b=b, out=n);
Nand(a=n, b=n, out=out);
}
TABLE And;
It prints
a b | out 0 0 | 0 0 1 | 0 1 0 | 0 1 1 | 1
Try it yourself
Why does Nand then Not give you And?
- Nand is And with the answer flipped, so flipping it again puts it back
- Because Not makes everything 1
- Because two gates are always better than one
- It does not — it gives Or
What does this print?
HDL
CHIP And {
IN a, b;
OUT out;
PARTS:
Nand(a=a, b=b, out=n);
Nand(a=n, b=n, out=out);
}
USE And;
SET a=1, b=0;
SHOW out;
SET b=1;
SHOW out;
Answer them in the app
🔀 Either
Or asks: is at least one of them on?
Or gives 1 when a is 1, or b is 1, or both.
The trick is sneaky. Nand says 0 only when *both* its inputs are 1. So if we flip a and b first, Nand says 0 only when both were off — which means it says 1 whenever at least one was on.
That is Or.
HDL
CHIP Or {
IN a, b;
OUT out;
PARTS:
Not(in=a, out=na);
Not(in=b, out=nb);
Nand(a=na, b=nb, out=out);
}
TABLE Or;
It prints
a b | out 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 1
You just used De Morgan’s law
"Not both off" means exactly the same thing as "at least one on". A man called Augustus De Morgan wrote that down in 1847, long before anyone had a computer to build with it.
That is why the wiring works. It is not a trick — it is the same sentence said two ways.
Try it yourself
a is 0 and b is 0. What does Or say?
- 0
- 1
- Both
- Neither
Answer it in the app
⚡ Different
Xor asks: are they different?
Xor gives 1 when a and b disagree, and 0 when they match.
Say it as two conditions joined together: *at least one is on* (that is Or), and *not both are on* (that is Nand). And them together.
HDL
CHIP Xor {
IN a, b;
OUT out;
PARTS:
Or(a=a, b=b, out=either);
Nand(a=a, b=b, out=notboth);
And(a=either, b=notboth, out=out);
}
TABLE Xor;
It prints
a b | out 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 0
Remember this one
Xor is the gate you will meet again in three lessons’ time, doing the most useful job in the whole track: adding.
0 + 0 = 0. 0 + 1 = 1. 1 + 0 = 1. 1 + 1 = 0, carry the 1.
Compare that middle column to Xor’s. They are the same.
Try it yourself
Both inputs are 1. What does Xor say?
- 0, because they are the same
- 1, because both are on
- 2
- It depends
What does this print?
HDL
CHIP Xor {
IN a, b;
OUT out;
PARTS:
Or(a=a, b=b, out=either);
Nand(a=a, b=b, out=notboth);
And(a=either, b=notboth, out=out);
}
USE Xor;
SET a=1, b=1;
SHOW out;
SET b=0;
SHOW out;
Answer them in the app
🚫 Neither
Nor asks: are both of them off?
Nor is Or with the answer flipped. It says 1 only when nothing is on at all.
By now you can probably guess the wiring before you read it.
HDL
CHIP Nor {
IN a, b;
OUT out;
PARTS:
Or(a=a, b=b, out=either);
Not(in=either, out=out);
}
TABLE Nor;
It prints
a b | out 0 0 | 1 0 1 | 0 1 0 | 0 1 1 | 0
Nand was not the only choice
Nor can build everything too — Not, And, Or, all of it. Either gate on its own is enough for a whole computer.
Chip factories picked Nand mostly because of how transistors behave. Either way, the point stands: one gate, repeated, is a computer.
Try it yourself
Which gate says 1 in exactly one row of its table?
- Nor — only when both inputs are 0
- Or — only when both inputs are 1
- Xor — only when the inputs match
- Not — it has no rows
Answer it in the app
🏆 The Gate Family
Try it yourself
Which gate answers "are these two wires carrying different things?"
- Xor
- And
- Or
- Nor
What does this print?
HDL
CHIP Test {
IN a, b;
OUT out;
PARTS:
Xor(a=a, b=b, out=diff);
Not(in=diff, out=out);
}
USE Test;
SET a=1, b=1;
SHOW out;
SET a=0;
SHOW out;
Answer them in the app