Building Logic Gate Circuits
In the previous lecture we got to know the four basic logic gates. In this lecture, we connect them together to build circuits that solve real problems.
A single logic gate is like a screw -- limited in function by itself. But combined, they can build arbitrarily complex functions.
Real-life analogy: building a castle with LEGO bricks
Imagine you have LEGO bricks in four colors in front of you:
- Green brick = AND gate: only conducts when both ends are connected
- Orange brick = OR gate: conducts when either end is connected
- Red brick = NOT gate: on becomes off, off becomes on
- Purple brick = XOR gate: conducts when the two ends differ
What can you build with these bricks?
A voting machine: three people vote, the majority wins. This is a typical combinational logic circuit -- connecting AND/OR gates can implement it.
A combination lock: several correct keys must be pressed simultaneously to unlock. This is essentially a cascade of AND gates -- each key corresponds to one input, and the output is 1 only when all inputs are 1.
An alarm: any sensor triggering sounds the alarm. This is essentially an application of the OR gate -- as long as one sensor outputs 1, the alarm sounds.
Combinational logic vs sequential logic
Before diving into circuit building, let's first distinguish two important concepts:
| Type | Feature | Output depends on | Has memory? | Example |
|---|---|---|---|---|
| Combinational logic | The output is determined only by the current inputs | Current input values | No | Adders, majority voters, decoders |
| Sequential logic | The output is determined by the current inputs + historical state | Current input + previously stored values | Yes | Counters, registers, state machines |
This lecture focuses on combinational logic. Sequential logic will be introduced in detail in Lecture 9.
A simple way to tell: combinational logic is "whatever you input, I output" (like a mathematical function); sequential logic is "I still remember what you input before" (like human memory).
Circuit building methodology: a three-step approach
To build any combinational logic circuit with logic gates, there is a general three-step process:
- List the truth table: list all combinations of inputs and write the expected output for each case.
- Write the logic expression: for each row where the output is 1, write the corresponding "AND term" (the AND combination of the inputs), then connect all AND terms with OR. This is called the "Sum of Products" form.
- Draw the circuit diagram / write the code: translate the logic expression into gate connections.
Below we practice this method with three examples from simple to advanced.
Case 1: two-input combination lock
Requirement: design a simple password-check circuit. There are two inputs A and B, and the output is 1 (unlock) only when A=1 and B=1; in all other cases the output is 0 (lock).
This requirement is actually just an AND gate. But we can use it to demonstrate the three-step method:
Step 1: truth table
| A | B | Expected output Y |
|---|---|---|
| 0 | 0 | 0 (lock) |
| 0 | 1 | 0 (lock) |
| 1 | 0 | 0 (lock) |
| 1 | 1 | 1 (unlock) |
Step 2: logic expression
Only the last row has output 1. The corresponding AND term is: A AND B.
So: Y = A AND B
Step 3: circuit implementation
Only one AND gate is needed, with A and B as inputs, and the output is Y.
Too simple? Let's look at an example with practical significance.
Case 2: majority voter (core case)
Requirement: three people (A, B, C) vote on a proposal (1 means approve, 0 means oppose). When at least two approve, the proposal passes (output 1).
This is a classic three-input majority voter, and a must-learn case in digital circuit textbooks.
Step 1: list the truth table
Three inputs have 2^3 = 8 combinations in total:
| A | B | C | Number of approvers | Whether it passes Y | Description |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | No one approves |
| 0 | 0 | 1 | 1 | 0 | Only C approves |
| 0 | 1 | 0 | 1 | 0 | Only B approves |
| 0 | 1 | 1 | 2 | 1 | B and C approve |
| 1 | 0 | 0 | 1 | 0 | Only A approves |
| 1 | 0 | 1 | 2 | 1 | A and C approve |
| 1 | 1 | 0 | 2 | 1 | A and B approve |
| 1 | 1 | 1 | 3 | 1 | Unanimously approved |
Step 2: write the logic expression
Find the rows with output 1 (rows 4, 6, 7, 8), and write an AND term for each row:
- Row 4: NOT(A) AND B AND C
- Row 6: A AND NOT(B) AND C
- Row 7: A AND B AND NOT(C)
- Row 8: A AND B AND C
Connect with OR: Y = (NOT(A) AND B AND C) OR (A AND NOT(B) AND C) OR (A AND B AND NOT(C)) OR (A AND B AND C)
This expression looks a bit long, but it is logically very clear: Y is 1 if and only if any two or three of the inputs are 1.
Step 3: draw the circuit diagram with SVG
Three AND gates respectively detect whether the "AB", "BC", "AC" pairs are 11, then the OR gate aggregates the results.
Interactive demo: Python implementation of the majority voter
Example
Three-input majority voter (runoob demo)
Requirement: three people vote; it passes (output 1) only when at least two approve (1)
----- Reuse the basic logic gates from Lecture 6 -----
def
AND
(
a
,
b
)
:
return
1
if
a
==
1
and
b
==
1
else
0
def
OR
(
a
,
b
)
:
return
1
if
a
==
1
or
b
==
1
else
0
def
NOT
(
a
)
:
return
1
if
a
==
0
else
0
----- Method 1: Sum of Products (SOP) standard form -----
def
majority_voter_sop
(
a
,
b
,
c
)
:
"""
Sum-of-products implementation of the majority voter.
Derive directly from the truth table: find all rows whose output is 1,
write one AND term for each row, then join them with OR.
"""
term1
=
AND
(
AND
(
NOT
(
a
)
,
b
)
,
c
)
~A·B·C
term2
=
AND
(
AND
(
a
,
NOT
(
b
)
)
,
c
)
A·~B·C
term3
=
AND
(
AND
(
a
,
b
)
,
NOT
(
c
)
)
A·B·~C
term4
=
AND
(
AND
(
a
,
b
)
,
c
)
A·B·C
return
OR
(
OR
(
term1
,
term2
)
,
OR
(
term3
,
term4
)
)
----- Method 2: optimized simplified form -----
def
majority_voter_optimized
(
a
,
b
,
c
)
:
"""
Optimized implementation of the majority voter: Y = AB + BC + AC.
This form needs fewer gates, but is logically equivalent to Method 1.
Why are they equivalent?
After expanding Method 1's SOP form, it can be simplified using the absorption law of Boolean algebra:
~A·B·C + A·~B·C + A·B·~C + A·B·C
= (~A·B·C + A·B·C) + (A·~B·C + A·B·C) + (A·B·~C + A·B·C)
= B·C·(~A+A) + A·C·(~B+B) + A·B·(~C+C)
= B·C + A·C + A·B
"""
return
OR
(
OR
(
AND
(
a
,
b
)
,
AND
(
b
,
c
)
)
,
AND
(
a
,
c
)
)
============================================
Print the full truth table and compare the two methods
============================================
(
"="
60
)
(
" 3-input majority voter truth table (RUNOOB demo)"
)
(
"="
60
)
(
)
(
"┌───┬───┬───┬────────┬──────────┬──────────┐"
)
(
"│ A │ B │ C │ Pass? │ SOP method │ Optimized method │"
)
(
"├───┼───┼───┼────────┼──────────┼──────────┤"
)
for
a
in
[
0
,
1
]
:
for
b
in
[
0
,
1
]
:
for
c
in
[
0
,
1
]
:
count
=
a + b + c
passed
=
"Passed"
if
count
=
2
else
"Not passed"
sop
=
majority_voter_sop
(
a
,
b
,
c
)
opt
=
majority_voter_optimized
(
a
,
b
,
c
)
(
f
"│ {a} │ {b} │ {c} │ {passed} │ {sop} │ {opt} │"
)
(
"└───┴───┴───┴────────┴──────────┴──────────┘"
)
============================================
Gate usage statistics
============================================
(
"\n"
"="
60
)
(
" Comparison of the number of gates used by the two methods"
)
(
"="
60
)
(
)
(
"Method 1 (full SOP form):"
)
(
" NOT gates: 3 (~A, ~B, ~C)"
)
(
" AND gates: 4 (four AND terms, each needs 2 two-input AND gates)"
)
(
" OR gates: 3 (combine the four AND terms)"
)
(
" Total: about 14 basic gate calls"
)
(
)
(
"Method 2 (optimized form Y = AB + BC + AC):"
)
(
" AND gates: 3 (AB, BC, AC)"
)
(
" OR gates: 2 (combine the three AND terms)"
)
(
" Total: 5 basic gate calls"
)
(
)
(
"After optimization, gate usage dropped by about 65%!"
)
(
"This is the engineering value of Boolean algebra simplification."
)
Note the process of converting a truth table into a logic expression. This is one of the most core skills in digital circuit design: see a truth table, write the expression; see an expression, draw the circuit diagram. The three correspond one-to-one.
Case 3: one-bit comparator
Requirement: compare two 1-bit binary numbers A and B. Output three signals: A>B, A=B, A
This is a very important basic circuit -- multi-bit comparators and conditional judgments in CPUs are all extended from here.
Step 1: truth table
| A | B | A > B | A = B | A < B |
|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 1 | 0 |
Step 2: logic expression
Observe the truth table:
- A > B is 1 if and only if: A=1 AND B=0. So: Greater = A AND NOT(B)
- A = B is 1 if and only if: A and B are the same. So: Equal = NOT(A XOR B), or in other words Equal = (A AND B) OR (NOT(A) AND NOT(B))
- A < B is 1 if and only if: A=0 AND B=1. So: Less = NOT(A) AND B
Note that detecting A = B is closely related to the XOR gate. XOR output 1 means 'different'; taking NOT of it gives 'equal'.
Step 3: code implementation
Example
One-bit comparator (runoob demo)
Compare two 1-bit binary numbers A and B
def
AND
(
a
,
b
)
:
return
1
if
a
==
1
and
b
==
1
else
0
def
OR
(
a
,
b
)
:
return
1
if
a
==
1
or
b
==
1
else
0
def
NOT
(
a
)
:
return
1
if
a
==
0
else
0
def
one_bit_comparator
(
a
,
b
)
:
"""
One-bit comparator: compare A and B.
Return a triple (Greater, Equal, Less)
"""
greater
=
AND
(
a
,
NOT
(
b
)
)
A > B: A=1 and B=0
less
=
AND
(
NOT
(
a
)
,
b
)
A < B: A=0 and B=1
A = B: neither greater nor less
Equivalent to NOT(A XOR B), implemented here in another way
equal
=
AND
(
NOT
(
greater
)
,
NOT
(
less
)
)
return
(
greater
,
equal
,
less
)
Print the truth table
(
"="
50
)
(
" One-bit comparator truth table (RUNOOB demo)"
)
(
"="
50
)
(
)
(
"┌───┬───┬────────┬────────┬────────┐"
)
(
"│ A │ B │ A > B │ A = B │ A < B │"
)
(
"├───┼───┼────────┼────────┼────────┤"
)
for
a
in
[
0
,
1
]
:
for
b
in
[
0
,
1
]
:
gt
,
eq
,
lt
=
one_bit_comparator
(
a
,
b
)
(
f
"│ {a} │ {b} │ {gt} │ {eq} │ {lt} │"
)
(
"└───┴───┴────────┴────────┴────────┘"
)
Test some useful scenarios
(
"\nTest scenarios:"
)
a_test
,
b_test
=
1
,
0
gt
,
eq
,
lt
=
one_bit_comparator
(
a_test
,
b_test
)
(
f
"A={a_test}, B={b_test}: A>B={gt}, A=B={eq}, A
)
(
f
"Interpretation: A is greater than B"
if
gt
else
(
"A equals B"
if
eq
else
"A is less than B"
)
)
Case 4: 2-to-4 decoder
Requirement: input a 2-bit binary number (00, 01, 10, 11), make exactly one of the 4 output lines 1 and the rest 0.
The decoder is a core component in CPUs -- when the CPU wants to select a register, it is essentially using a decoder.
Truth table
| A1 | A0 | Y0 | Y1 | Y2 | Y3 |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 0 | 1 |
Logical expression
- Y0 = NOT(A1) AND NOT(A0)
- Y1 = NOT(A1) AND A0
- Y2 = A1 AND NOT(A0)
- Y3 = A1 AND A0
Example
2-to-4 decoder (runoob demo)
Input: 2-bit binary number A1 A0
Output: exactly one of the 4 lines is 1
def
AND
(
a
,
b
)
:
return
1
if
a
==
1
and
b
==
1
else
0
def
NOT
(
a
)
:
return
1
if
a
==
0
else
0
def
decoder_2to4
(
a1
,
a0
)
:
"""
2-line to 4-line decoder.
Return a list [Y0, Y1, Y2, Y3],
where exactly one is 1 and the rest are 0.
"""
Y0
=
AND
(
NOT
(
a1
)
,
NOT
(
a0
)
)
00 → Y0
Y1
=
AND
(
NOT
(
a1
)
,
a0
)
01 → Y1
Y2
=
AND
(
a1
,
NOT
(
a0
)
)
10 → Y2
Y3
=
AND
(
a1
,
a0
)
11 → Y3
return
[
Y0
,
Y1
,
Y2
,
Y3
]
Print the truth table
(
"="
50
)
(
" 2-to-4 decoder truth table (RUNOOB demo)"
)
(
"="
50
)
(
)
(
"┌────┬────┬────┬────┬────┬────┐"
)
(
"│ A1 │ A0 │ Y0 │ Y1 │ Y2 │ Y3 │"
)
(
"├────┼────┼────┼────┼────┼────┤"
)
for
a1
in
[
0
,
1
]
:
for
a0
in
[
0
,
1
]
:
Y
=
decoder_2to4
(
a1
,
a0
)
(
f
"│ {a1} │ {a0} │ {Y[0]} │ {Y[1]} │ {Y[2]} │ {Y[3]} │"
)
(
"└────┴────┴────┴────┴────┴────┘"
)
Show a real-world usage scenario
(
"\nReal-world scenario: use a decoder to select a register"
)
registers
=
[
"R0"
,
"R1"
,
"R2"
,
"R3"
]
for
a1
in
[
0
,
1
]
:
for
a0
in
[
0
,
1
]
:
Y
=
decoder_2to4
(
a1
,
a0
)
idx
=
Y.
index
(
1
)
Find which output line is 1
(
f
" Address A1A0={a1}{a0} -> select register {registers[idx]}"
)
The decoder is the classic 'one-to-many' circuit. Its counterpart is the multiplexer (MUX), which is 'many-to-one'. Both are very common in CPU datapath design. A decoder is used to 'select a destination' (e.g. which register to write), while a multiplexer is used to 'select a source' (e.g. which register the ALU reads data from).
Universal gates: what makes NAND and NOR special
In real chip manufacturing, NAND and NOR gates are more 'fundamental' than AND and OR gates.
NAND gate = AND + NOT, i.e. do AND first then negate. NAND and NOR are called 'universal gates' because each of them alone can implement all Boolean functions.
Why do chips favor NAND/NOR? Because in CMOS technology:
- A NAND gate needs only 4 transistors, while an AND gate needs 6 (AND = NAND + NOT)
- NAND gates switch faster
- Therefore the AND function in real chips is implemented at the bottom with NAND + NOT
Below we verify the 'universality' of the NAND gate:
Example
Prove the universality of the NAND gate (runoob demo)
A NAND gate alone can implement the three basic functions NOT, AND, OR
def
NAND
(
a
,
b
)
:
"""NAND gate: negate the result of AND.
Y = 0 if and only if a=1 and b=1"""
return
0
if
a
==
1
and
b
==
1
else
1
Implement NOT with NAND: NOT(A) = NAND(A, A)
def
NOT_from_NAND
(
a
)
:
return
NAND
(
a
,
a
)
Implement AND with NAND: AND(A,B) = NOT(NAND(A,B)) = NAND(NAND(A,B), NAND(A,B))
def
AND_from_NAND
(
a
,
b
)
:
nand_ab
=
NAND
(
a
,
b
)
return
NAND
(
nand_ab
,
nand_ab
)
Implement OR with NAND: OR(A,B) = NAND(NOT(A), NOT(B))
def
OR_from_NAND
(
a
,
b
)
:
return
NAND
(
NOT_from_NAND
(
a
)
,
NOT_from_NAND
(
b
)
)
============================================
Validation
============================================
(
"="
60
)
(
" Prove the universality of the NAND gate (RUNOOB demo)"
)
(
"="
60
)
(
"\n[Verification: NOT implemented with NAND]"
)
for
a
in
[
0
,
1
]
:
expected
=
1
if
a
==
0
else
0
actual
=
NOT_from_NAND
(
a
)
(
f
" A={a}: NOT_from_NAND = {actual}, expected = {expected}, {'Pass' if actual == expected else 'Fail'}"
)
(
"\n[Verification: AND implemented with NAND]"
)
for
a
in
[
0
,
1
]
:
for
b
in
[
0
,
1
]
:
expected
=
1
if
a
==
1
and
b
==
1
else
0
actual
=
AND_from_NAND
(
a
,
b
)
(
f
" A={a}, B={b}: AND_from_NAND = {actual}, expected = {expected}, {'Pass' if actual == expected else 'Fail'}"
)
(
"\n[Verification: OR implemented with NAND]"
)
for
a
in
[
0
,
1
]
:
for
b
in
[
0
,
1
]
:
expected
=
1
if
a
==
1
or
b
==
1
else
0
actual
=
OR_from_NAND
(
a
,
b
)
(
f
" A={a}, B={b}: OR_from_NAND = {actual}, expected = {expected}, {'Pass' if actual == expected else 'Fail'}"
)
(
"\nConclusion: with just the NAND gate alone, you can construct ANY Boolean function."
)
(
"This is why the nand2tetris course can build an entire computer starting from the NAND gate."
)
nand2tetris (from NAND to Tetris) is a famous computer science course. It challenges you to use only one component, the NAND gate, to step by step build a CPU, memory, an operating system, and finally run a Tetris game. If you are interested in the low level of computers, it is highly recommended.
Interactive demo: majority voter circuit builder (runoob vis-network demo)
Below uses vis-network visualizes a three-input majority voter circuit (Y = AB + BC + AC). Click input nodes A, B, C to toggle 0/1; the circuit automatically evaluates and updates all wire colors -- red means signal 1, gray means signal 0.
Three-input majority voter (click A/B/C nodes to toggle)
A=0, B=0, C=0 -> not passed (needs at least 2 approvals)
Input node A
Input node B
Input node C
AND gate
OR gate
Output Y