L1 · HardwareHow Computers Work · part 1 of 4

How a Computer Works: From Sand to Software

Transistor, gate, adder, clock, memory, program: the six layers between electricity and the software you run.

Takumi Yamada
· 6 min read
hardwaretransistorslogic-gatesarchitecturefundamentals

We use computers every day, but the inside is rarely explained from the bottom up. This post does that.

A computer is built in layers. There are about six of them, and each layer is built entirely out of the layer below it. Electricity is at the bottom and your editor is at the top. This post goes through the layers one at a time, starting from the bottom.

6SOFTWAREprograms and the operating system5ARCHITECTUREthe CPU: fetch, decode, execute4MEMORYremembering bits3ARITHMETICadding bits2LOGIC GATESdeciding bits1TRANSISTORSswitching electronsphysicsSILICONsand, refined
The tower this post climbs, from the bottom up.

Layer 1: The transistor, a switch with no moving parts

Everything rests on one device. A transistor is a simple switch where a on one wire (the gate) controls whether current can flow between the other two. If the voltage is present, it conducts electricity. If the voltage is absent, it stops the flow.

In daily life this is the light switch on your wall. It's on, or it's off. There is nothing in between.

OFFthis state is named 0ONthis state is named 1LIGHT SWITCHa finger moves itTRANSISTORa voltage on the gatewire operates itgate: no voltagesourcedraingate: voltage onsourcedrainThe gate does not touch the other two wires. Only its voltage matters.
A transistor does the job of a light switch, but a voltage operates it where the light switch needs a finger. The two wires that carry the current are called the source and the drain.
A small black electronic part with three metal legs, lying on a white surface.
One transistor sold as a separate part, about a centimetre wide. Each of its three legs is one of the three wires.Photo: Retired electrician, CC0 1.0, via Wikimedia Commons (original). Changes: resized.

A transistor only has two states, so we name them 1 and 0. It can switch billions of times per second, it never wears out, and it can be made smaller than a virus. A modern chip contains tens of billions of them. Everything your computer does is made of these transistors switching on and off.

Left: a processor as it is sold, a square metal lid printed with Intel Core i5-11400. Right: the same processor with the lid removed, showing a dark rectangle of silicon on a green board.
A processor with its metal lid on, and the same one with the lid taken off. The dark rectangle is the chip itself. All of its transistors are on that one piece of silicon, and each one is far too small to see.Photo: Fritzchens Fritz, CC0 1.0, via Wikimedia Commons (original 1, original 2). Changes: two photos cropped and placed side by side.
Left: a handful of pale sand. Right: a shiny, dark grey lump of silicon with sharp edges.
Left: sand, which is mostly quartz, a compound of silicon and oxygen. Right: pure silicon, about 2 cm across. Quartz is heated with carbon in a furnace to remove the oxygen, and the silicon is then cleaned with chemicals until fewer than one atom in a billion is something else.Left photo: freestocks.org, CC0 1.0, via Wikimedia Commons (original). Changes: cropped, resized and placed beside another photo.Right photo: Jurii, CC BY 3.0, via Wikimedia Commons (original). Changes: resized and placed beside another photo.
Left: a thick dark grey cylinder of silicon in a museum display, with a pointed top where a thin rod sticks up. Right: a round, mirror-like disc covered in a grid of tiny squares that reflect rainbow colours.
Left: an ingot, which is one large crystal of silicon. It grows when a small seed crystal is dipped into melted silicon and slowly pulled up. Right: a wafer, a slice of an ingot less than a millimetre thick. Hundreds of chips are made on one wafer at the same time, one in each small square, and then cut apart.Left photo: ArticCynda, CC0 1.0, via Wikimedia Commons (original). Changes: cropped, resized and placed beside another photo.Right photo: Rob Bulmahn, CC BY 2.0, via Wikimedia Commons (original). Changes: cropped, resized and placed beside another photo.

Layer 2: Logic gates, switches that make decisions

If we wire a few together, we get gates, which are tiny circuits that answer basic logical questions.

  • AND: the output is 1 only if both inputs are 1. Picture two switches in a row.
  • OR: the output is 1 if either input is 1. Picture two switches side by side.
  • NOT: the output is the exact opposite of the input.
aba AND ba OR bNOT a
00001
01011
10010
11110
ANDtwo switches in a rowa = 1b = 0out = 0the current stops at the open switchORtwo switches side by sidea = 1b = 0out = 1
The same two inputs through each gate. A closed switch is a 1, and the lit wires are the ones carrying current.

These gates can be connected together. The output of one feeds directly into the input of the next. Do that enough times and you can build any function on that you want. Everything above this layer is just millions of gates arranged very carefully by engineers.

a = 1b = 1ANDboth on?1NOTthe oppositeout = 0The output of AND is the input of NOT.
Two gates connected. AND outputs 1 because both inputs are 1, and NOT turns that 1 into 0.

Layer 3: Arithmetic, teaching electricity to do math

This layer answers a common question. How do switches that only turn on and off calculate anything?

It starts with the addition we do on paper. When we add 9 + 1 on paper, we write down 0 and carry the 1 to the next column. Computers do exactly the same thing, but they only have two digits, 0 and 1. So when a computer adds 1 + 1, the answer is 2, which is written as 10 in binary. The sum is 0, and we carry the 1.

DECIMALten digits, 0 to 9carry19+1109 + 1 is ten.One column can't hold ten,so write 0 and carry 1.Here 10 is the number ten.BINARYtwo digits, 0 and 1carry11+1101 + 1 is two.One column can't hold two,so write 0 and carry 1.Here 10 is the number two.Zero to five, written in binary:0, 1, 10, 11, 100, 101
Carrying works the same way in both number systems. A decimal column is full at 9, and a binary column is full at 1.

To make electricity do this, engineers realized they could use the gates from layer 2.

  • The sum. An XOR (Exclusive OR) gate turns on only when the inputs are different, so 1 + 0 or 0 + 1.
  • The carry. An AND gate turns on only when both inputs are 1, so 1 + 1.

If we split two incoming wires and plug them into both gates at the same time, we get a circuit that genuinely adds.

a = 1b = 1XORinputs differ?ANDboth on?sum = 0carry = 11 + 1 is 10 in binary: write 0, carry 1
inputs
1 + 1 → sum 0, carry 1
Both inputs start on, and the lit wires are the ones carrying a 1. Press a or b to switch an input and see what the two gates give.

Send electricity through both wire a and wire b, which means 1 + 1. The XOR gate stays off, so the sum is 0. The AND gate turns on, so the carry is 1. That's the correct answer, produced by nothing but switches.

This small circuit of about a dozen transistors is called a half adder.

The rest is repetition. Connect 64 of these adders in a row, so that the carry wire of each one goes into the next one. This is the same as carrying numbers across columns on paper. The result is a 64-bit adder.

a bADDERbit 0suma bADDERbit 1suma bADDERbit 2suma bADDERbit 63sumbits 3 to 62Each accent arrow is a carry wire, going from one adder into the next.
A 64-bit adder. Bits 3 to 62 are left out of the drawing.

Bundle that with a few more circuits for subtracting, comparing, and shifting, and we have built the ALU, the Arithmetic Logic Unit. This is the calculator inside the (Central Processing Unit). The chip doesn't know math. Addition is built into the way its wires are connected.

Layer 4: Memory, gates that remember

Every circuit we have looked at so far forgets its inputs instantly. The trick for memory is a feedback loop. Wire two NOT gates in a circle and each one holds the other in place.

STORES 1NOTNOT10STORES 0NOTNOT01
The same loop in its two stable states. Whichever one it is in, it stays there.

This loop has two stable states. It keeps one bit of data until a new signal changes it.

That loop is called a latch. It stores exactly one bit. Put a few latches side by side and we make a register, which holds the numbers the CPU is working on right now. Put millions of these storage cells in a massive grid and we get RAM (Random Access Memory). To find a specific piece of data, the computer selects a row by its number. That row number is exactly what a memory address is, and it's the number a holds. The rest of how storage works, including , the stack, and the , is its own post: Memory: The Stack, the Heap, and Pointers.

ADDRESSEIGHT STORAGE CELLS, ONE BIT EACH001001000100000001211011000311000011411111111millions more rowsthe computerasks for row 2216the eight bits of thatrow, read as a number
RAM as a grid. The address is the number of the row. A row here is eight bits, which is called one byte.
A long green circuit board with eight black rectangular chips on it and a row of gold contacts along the bottom edge.
A memory module from a desktop computer. Each black rectangle is a memory chip that holds about eight billion bits.Photo: PantheraLeo1359531, CC BY 4.0, via Wikimedia Commons (original). Changes: resized.

Layer 5: The clock and the stored program

Two final ideas turn this pile of circuits into a working computer.

The first is the clock. It's a signal that switches between 0 and 1 billions of times per second, and that's what 3.5 GHz means. The timing comes from a small quartz crystal on the motherboard, which vibrates millions of times per second, and other circuits multiply that up to billions. On every tick the whole machine advances one step together. It computes, stores the results, and computes again. The clock is what keeps billions of transistors in sync.

10ticktickticktickone step: compute,then store the resultsAt 3.5 GHz there are 3.5 billion ticks every second.
The clock is a signal that switches between 0 and 1 at a fixed rate. Every part of the machine takes its next step on the same tick.
A small oval metal case soldered to a green circuit board, stamped 25.000.
A quartz crystal in its metal case, on a circuit board. The 25.000 stamped on it means it vibrates 25 million times per second. Other circuits multiply that steady rate up to the billions of ticks per second that a processor runs at.Photo: Slick, CC0 1.0, via Wikimedia Commons (original). Changes: resized.

The second is the stored program, and it's the most important idea in computing. Instructions are just numbers sitting in memory, right next to the data. There is no physical difference between the of a program and the bytes of a photograph. The only difference is how the CPU treats them.

The CPU keeps a counter pointing at the next instruction. On every tick of the clock it fetches that number, decodes which circuit it needs to activate, executes the command, and repeats. Because programs are just data, you can write programs that write other programs. That's all a is, and it's why software can build software. The full loop, pipelines and all, is in How a CPU Actually Runs Your Code.

read as instructions:add two numbers, then returnread as data:the first bytes of a JPEG photo72011216219532554216525562247addressCOUNTERholds 0The CPU's counter holds an address. The number at thataddress is fetched and treated as an instruction.
Eight numbers in memory. The first four are machine code for an x86-64 processor, the kind in most desktop PCs. The last four are how a JPEG file begins. The number 216 is in both groups, and nothing about it says which kind it is.

Layer 6: Software, where the tower gets abstract

From here there are more layers, but they are made of code instead of hardware.

YOUR APP“save the file”RUNTIME, LIBRARIESbuffered writes, string handlingOPERATING SYSTEMfilesystems, scheduling, memory protectionMACHINE CODEthe numbers the CPU fetchesHARDWARE BOUNDARYCPU + RAM + I/Oeverything built in layers 1 to 5
The layers above the hardware. Each one only talks to the layers next to it.

Each layer only speaks to the layers directly next to it. This separation is what makes the whole system possible to understand. The doesn't think about individual transistors, and your text editor doesn't think about carry bits.

When you press one key, every layer does its part in order. The keyboard sends an , the operating system catches it, the editor code processes the letter, and the screen updates. That's hundreds of billions of transistor switches per second while you type a sentence. Each one is a switch doing exactly what layer 1 described.

HARDWAREKEYBOARDkey goes downOPERATING SYSTEMDRIVERcatches itYOUR APPEDITORhandles the letterHARDWARESCREENshows the letterinterruptEach step is carried out by transistors switching on and off, as in layer 1.
One key press. It starts in hardware, goes up through the operating system to the app, and ends in hardware again.

The Bottom Line

You can program for years without knowing these layers, and most of the time that works. They start to matter when you hit a performance problem, a bug, or a .

I build tools in Go on Arch, and a desktop environment from the ground up, and the single most useful habit I know is asking one question. Which layer is this problem actually on? Slow code is usually a memory question. A hang is usually a scheduling question. A crash is usually a stored program question, where some bytes got treated as the wrong kind of number.

Switch, gate, adder, latch, clock, stored program. Those six layers are the whole machine.

Check yourself

3 questions · nothing is saved
  1. 1.When a half adder adds 1 + 1, what do its two gates give?
  2. 2.What lets a circuit keep a bit after its input has gone away?
  3. 3.What is the physical difference between the bytes of a program and the bytes of a photo in memory?

Who wrote this

Author profile and background

Takumi Yamada

Takumi Yamada

· Backend Engineer

Backend engineer building services, databases, Linux tooling, and NEGEN SYSTEMS. Writing about low-level mechanics, distributed systems, and real-world security.

End of post
NEGEN SYSTEMS
Receipt · post 3 of 31
Written
Nov 1, 2025
Length
1,337 words · 6 min
Built
…
Served by
…
Delivered
…
Over the wire
…
Go's share
…
Next in How Computers Work · part 2 of 4

USB Sticks, Disks, and the Mysterious 4GB Wall

Why a 64GB USB stick refuses a 5GB file, what a file system actually is, and how FAT32, exFAT and NTFS really differ inside.

8 min read