How Computers Work — Binary & Logic Gates
Middle SchoolA digital computer is a machine that manipulates information encoded as voltage. It recognises only two stable states — high and low — which we write as 1 and 0. A single such state is a bit (binary digit), the atom of all computing. Everything a computer stores or does — numbers, text, images, this very page — is ultimately a pattern of bits.
Bits are combined by logic gates: tiny circuits whose output is a Boolean function of their inputs. The complete algebra was formalised by George Boole (1847) and connected to switching circuits by Claude Shannon (1937). The three fundamental gates are:
| A | B | AND (A·B) | OR (A+B) | NOT A (Ā) |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 |
From these you can build NAND and NOR, each of which is functionally complete: any Boolean function whatsoever can be built from NAND gates alone. This is the deep reason a uniform sea of identical transistors can compute anything computable.
Why two states and not ten? Because reliability beats density. A wire carrying "about 0 volts" or "about 3 volts" tolerates noise, temperature drift and manufacturing error; a wire meant to hold ten distinct voltage levels does not. Binary is the engineering sweet spot: the fewest symbols that can still encode everything, chosen because they are the easiest to tell apart.
A gate is physically a few transistors acting as voltage-controlled switches. XOR ("exclusive or") outputs 1 when its inputs differ — this is the gate that detects a change, and, as you will see, the gate that adds. The magic of digital design is composition: wire the output of one gate into the input of another and complex behaviour emerges from trivially simple parts, with no new physics required at each level.
Big idea. A computer is not "smart matter." It is an enormous, layered arrangement of switches obeying a two-valued algebra. Understanding follows from tracing that algebra — never from magic.
Let us build a 1-bit half adder — the circuit that adds two bits and produces a sum and a carry — using only Boolean logic, then chain it. Adding 1 + 1 gives binary 10: sum 0, carry 1. Note that sum = A XOR B and carry = A AND B.
# Half adder and full adder from primitive gates.
# We model gates as functions on 0/1.
def AND(a, b): return 1 if (a and b) else 0
def OR(a, b): return 1 if (a or b) else 0
def NOT(a): return 1 - a
def XOR(a, b): return AND(OR(a, b), NOT(AND(a, b))) # (A or B) and not(A and B)
def half_adder(a, b):
return XOR(a, b), AND(a, b) # (sum, carry)
def full_adder(a, b, carry_in):
s1, c1 = half_adder(a, b)
s2, c2 = half_adder(s1, carry_in)
return s2, OR(c1, c2) # (sum, carry_out)
# Ripple-carry adder: add two n-bit numbers, LSB first.
def add_bits(A, B): # A, B are bit lists, LSB at index 0
n = max(len(A), len(B))
A = A + [0] * (n - len(A))
B = B + [0] * (n - len(B))
carry, result = 0, []
for i in range(n):
s, carry = full_adder(A[i], B[i], carry)
result.append(s)
result.append(carry) # final carry becomes the top bit
return result
# 3 (011) + 5 (101) = 8 (1000)
print(add_bits([1, 1, 0], [1, 0, 1])) # -> [0, 0, 0, 1] == 1000 binary
Step by step: XOR is defined from AND/OR/NOT; the half adder emits sum and carry; the full adder chains two half adders plus an OR; the ripple-carry loop threads the carry from each bit to the next — exactly how arithmetic hardware works.
Q1 Why is NAND called "universal"?
Because every Boolean function can be expressed using only NAND gates. For example NOT a = a NAND a, and a AND b = NOT(a NAND b) = (a NAND b) NAND (a NAND b). Since {AND, OR, NOT} is complete and all three reduce to NAND, so is NAND alone. Chip fabs love this: a single reusable gate type simplifies manufacturing.
Q2 How many rows does a truth table for n inputs have?
2^n, because each input independently takes 2 values. Three inputs give 8 rows; ten inputs give 1024. This exponential growth is why we reason with algebra and composition rather than enumerating tables for real circuits.
Q3 What is the difference between a half adder and a full adder?
A half adder adds two bits and cannot accept an incoming carry, so it can only ever be the least-significant stage. A full adder adds three bits — two operands plus a carry-in — and produces a carry-out, so full adders can be chained to add numbers of any width.
Q4 Do computers "understand" the 1s and 0s as numbers?
No. The hardware only routes voltages through gates. Meaning lives entirely in interpretation — the same bit pattern 01000001 is the number 65, the letter "A", or a shade of grey depending on what layer of software chooses to read it. Chapter 2 makes this precise.
How the ideas connect
Every key idea in this chapter, branching from the core concept — use it to see the whole picture at a glance.
The key facts, visualised
Worked problems, step by step
Follow each solution line by line, then try to reproduce it on paper before moving on.
Example 1Evaluate 1 AND 0, then 1 OR 0, then NOT 0.
- AND needs both 1, but one input is 0.
- OR needs at least one 1, and there is a 1.
- NOT flips 0.
Example 2A half adder adds 1 + 1. Give the sum bit and carry bit.
- Sum = A XOR B = 1 XOR 1 = 0.
- Carry = A AND B = 1 AND 1 = 1.
Now you try
Work each one out first, then tap to reveal the worked answer.