Chapter 06

How Computers Work — Binary & Logic Gates

Middle School
At a glance
Core ideaEverything reduces to bits (0/1) combined by tiny logic gates.
Key termBit — one two-state signal; the atom of all computing.
You can…Build a half and full adder from AND, OR and NOT.
Watch outNAND alone is universal — any circuit can be built from it.
Theory

A 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:

ABAND (A·B)OR (A+B)NOT A (Ā)
00001
01011
10010
11110

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.

1 1 AND 1 1 AND 1 = 1 1 0 OR 1 1 OR 0 = 1 1 NOT 0 NOT 1 = 0
Logic gates take one or two bits in and produce exactly one bit out, following a fixed rule.
Explanation

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.

Practical

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.

Q&A
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.

Concept mind map

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.

bit is 0 or 1AND gateOR gateNOT gatehalf adderfull adderBinary and logic gates
Infographic

The key facts, visualised

Bit
One two-state signal, 0 or 1; the atom of all computing.
AND
Output 1 only when both inputs are 1; otherwise 0.
OR
Output 1 when at least one input is 1; 0 only if both are 0.
XOR
Output 1 when inputs differ; used for the sum bit in adders.
Solved examples

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.

  1. AND needs both 1, but one input is 0.
  2. OR needs at least one 1, and there is a 1.
  3. NOT flips 0.

Example 2A half adder adds 1 + 1. Give the sum bit and carry bit.

  1. Sum = A XOR B = 1 XOR 1 = 0.
  2. Carry = A AND B = 1 AND 1 = 1.
Practice problem set

Now you try

Work each one out first, then tap to reveal the worked answer.

1What is a bit?
A bit is a single two-state signal that is either 0 or 1, the smallest unit of computing.
2When does an AND gate output 1?
Only when both of its inputs are 1.
3When does an OR gate output 0?
Only when both inputs are 0; any 1 input makes it output 1.
4What does a NOT gate do?
It flips its input: 0 becomes 1 and 1 becomes 0.
5What does a half adder compute?
It adds two bits, producing a sum bit (A XOR B) and a carry bit (A AND B).
6Why do computers use binary?
Because hardware reliably stores and switches two states (on/off), which map cleanly to 1 and 0.