Module 6 · Intermediate · Inside the hardware: from transistors to processors

Transistors: how a computer understands binary

How a computer understands binary: what a transistor is, how it works as a switch, why computers use 0 and 1, and how transistors build gates, adders and CPUs.

By theprogrammers team · · 10 min read

Every lesson so far has said "the computer only understands 0 and 1". But a chip of silicon doesn't understand anything. So what is a 0 inside the machine, what is a 1, and how can a lump of metal and sand add numbers or run Python? The answer starts with one tiny part, the transistor, and this lesson explains how a computer understands binary with it.

A transistor is a switch that electricity can turn on and off, with no moving parts, billions of times a second. A modern laptop chip has tens of billions of them. Join a few transistors together and you get a logic gate; join gates and you get circuits that add numbers, choose between signals and remember bits; join those and you get a CPU. This module climbs that ladder one rung at a time, and this first lesson shows the whole ladder so you can see how the parts relate.

What you need first: bits, bytes and binary and CPU, GPU, RAM and storage.

Key terms

  • Transistor: a tiny electronic switch where a small voltage on one terminal controls whether current flows between the other two.
  • Voltage: the electrical "pressure" on a wire, measured in volts (V). A high voltage means 1, a low one means 0.
  • Semiconductor: a material, usually silicon, that can be made to conduct electricity or block it. Transistors are made from it.
  • Integrated circuit (chip): many transistors and their connecting wires made together on one small piece of silicon.
  • Logic gate: a small circuit, built from a few transistors, that takes one or two bits in and gives one bit out by a fixed rule.
  • Abstraction: hiding the details of one layer so the next layer can be built on top without thinking about them.

0 and 1 are just voltages

Inside a chip, every wire is at one of two voltages:

Voltage on the wireRead asAlso called
Low, close to 0 volts0False, off
High, close to the chip's supply voltage (around 1 volt in modern CPUs)1True, on

That's what a bit physically is: a wire that's either "high" or "low". A byte is 8 such wires side by side, or 8 tiny places that each hold a high or low charge.

The letter "M", stored as the number 77, is the pattern below. Each 1 is a wire at high voltage; each 0 is a wire at low voltage:

number = ord("M")
bits = format(number, "08b")
print(number, bits)
print(["high" if b == "1" else "low" for b in bits])
77 01001101
['low', 'high', 'low', 'low', 'high', 'high', 'low', 'high']

The computer never "sees" the 77 or the "M". It only ever has eight wires at high or low voltage. Everything else is meaning that we, and our software, attach to those patterns.

Why two levels and not ten?

You could build a computer with ten voltage levels for the digits 0 to 9, and early machines tried it. Binary won because:

  • It's hard to get wrong. Electrical noise, heat and tiny manufacturing differences shift voltages a little. With only "high" and "low", there's a wide gap between them, so a slightly wrong voltage is still clearly one or the other. With ten levels packed close together, small errors would turn a 6 into a 7.
  • Switches are naturally two-state. A switch is on or off. Transistors are best at being fully on or fully off.
  • The maths is simple. All of logic can be built from "true" and "false", as the next lessons show.

What is a transistor?

A light switch on the wall has two wires and your finger: press it, and current flows to the bulb. A transistor is the same idea, except that instead of a finger, a voltage on a third wire flips the switch.

The kind used in almost every chip today is the MOSFET (metal–oxide–semiconductor field-effect transistor). It has three terminals:

TerminalJobLight-switch comparison
SourceWhere current comes inThe wire from the mains
DrainWhere current goes outThe wire to the bulb
GateControls the switch: a voltage here turns it on or offYour finger

Put a high voltage on the gate, and a thin channel forms in the silicon between source and drain, so current flows: the switch is on. Remove the voltage, and the channel disappears: the switch is off. The gate is insulated from the channel by a very thin layer of oxide, so almost no current flows into the gate itself.

This is the key trick: the output of one transistor can be the gate input of another. One switch can control the next, which controls the next. That's what lets transistors do logic, not just switch a bulb.

Two kinds, used in pairs

There are two types of MOSFET:

  • NMOS: turns on when the gate is high.
  • PMOS: turns on when the gate is low.

Chips pair them up, which is called CMOS (complementary MOS). The simplest CMOS circuit is a NOT gate (inverter), made of just one PMOS and one NMOS transistor:

          supply (1)
              |
        [PMOS: on when input is 0]
              |
 input ---+   +------- output
          |   |
        [NMOS: on when input is 1]
              |
          ground (0)
  • Input 0: the PMOS is on and connects the output to the supply, so the output is 1.
  • Input 1: the NMOS is on and connects the output to ground, so the output is 0.

The output is always the opposite of the input. Two transistors, and we have our first piece of logic. A NAND gate takes four transistors; an AND gate takes six. Because one of each pair is always off, current only flows at the moment of switching, which is why CMOS chips can hold billions of transistors without melting.

How small and how many

YearDeviceTransistors
1947First transistor, at Bell Labs1 (about the size of your thumb)
1971Intel 4004, the first single-chip microprocessorAbout 2,300
2020Apple M1About 16 billion
TodayLarge AI chipsOver 100 billion

Before transistors, computers used vacuum tubes: glass bulbs that did the same switching job, but were big, hot and burnt out often. ENIAC, finished in 1946, used about 17,500 of them and filled a large room.

You'll see chips described as "5 nm" or "3 nm". Those names are now labels for a generation of manufacturing, not the actual size of any part of the transistor, but the parts really are only tens of nanometres across: far smaller than a virus.

The pattern of transistor counts doubling about every two years is called Moore's law, after Intel co-founder Gordon Moore. It held for decades; it has slowed in recent years as transistors approach the size of a few dozen atoms.

The ladder: from transistors to Python

Here's how everything in this module, and in the course so far, connects. Each layer is built from the one below it, and each layer lets you forget the details underneath:

LayerBuilt fromDoesLesson
TransistorsSiliconSwitch on and offThis one
Logic gates (AND, OR, NOT, XOR)A few transistorsCombine bits by a ruleLogic gates and truth tables
Simplified circuitsGates, designed with Boolean algebra and K-mapsDo a job with as few gates as possibleBoolean algebra and K-maps
AddersXOR, AND and OR gatesAdd binary numbersHalf adder and full adder
Multiplexers and decodersAND, OR and NOT gatesChoose between signals, pick one of manyMultiplexers and decoders
Flip-flops, registers, ALU, control unitGates, adders and multiplexersStore bits, calculate, follow instructionsFrom logic gates to a CPU
Machine code and assemblyInstructions the CPU decodesTell the CPU what to do, one tiny step at a timeAssembly language for beginners
Memory and storageFlip-flops, capacitors, magnetism, flash cellsHold data while running, and keep it when the power is offHow RAM, hard disks and SSDs store data
A running program's memoryRAM, organised by the OSStack for function calls, heap for dataStack and heap memory
Operating systemMachine codeRun many programs, threads and tasksThreads, multithreading and multitasking
Processors and microcontrollersEverything above, on one chipRun laptops, phones, and the small computers in appliancesProcessor vs microcontroller
Your Python programThe interpreter and the OSWhatever you write; CPU profiling shows where its time goesThe Python course

When you write total = price + gst, the Python interpreter turns it into machine instructions; the CPU's control unit decodes them; multiplexers route price and gst from registers into an adder; the adder's gates combine the bits; and underneath, billions of transistors switch on and off. You never think about the transistors, and you don't need to. That's abstraction at work, and it's the main reason programming is possible at all.

Practice

  1. What does a bit physically look like inside a CPU?
Show the answer

A wire, or a tiny storage spot, at one of two voltages: low (close to 0 V) for 0, and high (about the supply voltage) for 1.

  1. Write the 8-bit pattern for the letter "A" (code 65), and list which positions are at high voltage, counting from the left starting at 1.
Show the answer
bits = format(ord("A"), "08b")
print(bits)
print([i + 1 for i, b in enumerate(bits) if b == "1"])
01000001
[2, 8]
  1. Give two reasons computers use binary instead of ten voltage levels.
Show the answer

Two levels are far apart, so noise and small manufacturing differences don't turn one value into another. Transistors work best fully on or fully off, and all logic can be built from true and false.

  1. In the CMOS NOT gate, which transistor is on when the input is 1, and what is the output?
Show the answer

The NMOS transistor is on (it switches on with a high gate), which connects the output to ground, so the output is 0.

  1. What does "the output of one transistor can control the gate of another" make possible?
Show the answer

Chains of switches controlling switches, which is what logic is. It lets transistors be combined into gates, and gates into adders, memory and CPUs, without anything mechanical.

Key takeaways

  • Inside a chip, 0 and 1 are low and high voltages on wires; the computer never sees numbers or letters, only these patterns.
  • Binary is used because two widely separated levels are reliable, and switches are naturally on or off.
  • A transistor is a switch controlled by a voltage on its gate; the output of one can control another.
  • CMOS pairs NMOS and PMOS transistors; two of them make a NOT gate, and a few more make every other gate.
  • Transistors build gates, gates build adders, multiplexers and memory, and those build the CPU your code runs on.

Frequently asked questions

How does a computer understand binary?

It doesn't "understand" in a human sense. Its circuits are built so that particular patterns of high and low voltages cause particular other patterns to appear. An adder circuit, for example, is wired so the output voltages always form the binary sum of the inputs. Meaning comes from how the circuits are arranged and how software interprets the results.

What is a transistor made of?

Mostly silicon, treated with tiny amounts of other elements (called doping) to control how it conducts, plus thin layers of insulating oxide and metal for the gate and wires. Billions are made at once on a round silicon wafer, which is then cut into chips.

Is a transistor the same as a logic gate?

No. A transistor is a single switch. A logic gate is a small circuit of several transistors that follows a rule, like AND or OR. A NOT gate needs 2 transistors; a NAND gate needs 4.

What to learn next

  1. Build logic from transistors in logic gates and truth tables.
  2. Revise how numbers become bits in bits, bytes and binary.
  3. See where it all ends up in CPU, GPU, RAM and storage.