# How Computers Work
## CS Foundations — The Codex Coding
**URL:** https://thecodex.expert/coding/foundations/how-computers-work/
**Type:** concept
**Confidence:** high
**Last verified:** 2026-06-01
**Sources:** 6

---

## CANONICAL DEFINITION

A computer is a machine that processes binary data using electronic circuits, executing sequences of stored instructions (a program) by repeatedly fetching an instruction from memory, decoding it, and executing it — a cycle that transforms input into output entirely through the switching of transistors between on and off states.

---

## CONCEPT RELATIONSHIP MAP

**Requires first:** None — this is an entry-level foundational page.

**Enables understanding of:**
- Binary and Number Systems
- Logic Gates
- CPU Architecture
- Memory
- Storage
- Operating Systems
- How Compilation Works
- How Interpretation Works

**Commonly confused with:** A calculator (fixed-function device, cannot be reprogrammed) versus a general-purpose computer (executes any stored program).

---

## CURIOUS LEVEL

A computer is a machine that follows instructions — and everything it does, from playing a video to running a nuclear simulation, comes down to billions of tiny switches turning on and off, billions of times per second.

### Transistors

Inside every computer there are billions of microscopic switches called transistors. Each transistor does exactly one thing: it is either on or off. A transistor on = 1. A transistor off = 0. Two possible states. Two digits. This is why computers use binary — not because it is clever, but because physical switches only have two positions. The entire digital world is built on that single constraint.

A modern smartphone contains around 15 billion transistors etched onto a chip smaller than a fingernail. A high-end desktop CPU contains over 50 billion.

### Binary

A single binary digit (called a bit) can hold two values: 0 or 1. Eight bits together form a byte. A byte can represent 256 different values — enough for every letter in the English alphabet, every digit, and every punctuation mark. The letter A is stored in memory as 01000001. The number 65 in a computer is also 01000001. What distinguishes them is not the bits, but how the program interprets them.

### Logic Gates

Connect a few transistors in the right arrangement and you get a logic gate — a circuit that takes one or two bits as input and produces one bit as output based on a fixed rule. An AND gate outputs 1 only when both inputs are 1. An OR gate outputs 1 if either input is 1. A NOT gate flips a bit. George Boole proved in 1847 that these three operations can perform any computation that is mathematically possible.

### The CPU

The Central Processing Unit (CPU) is the chip containing millions of logic gates arranged to perform arithmetic, compare values, move data, and make decisions. It needs instructions — a list of operations to perform in order. That list is a program. Programs are stored in memory.

### Memory and Storage

Memory (RAM) is where programs live while running. It is fast but temporary — cleared when power is removed. Storage (SSD, HDD) holds data permanently but is much slower.

### The Fetch-Decode-Execute Cycle

The CPU runs one loop continuously: Fetch (read the next instruction from memory), Decode (determine what the instruction means), Execute (perform the operation). This cycle repeats billions of times per second. Every program ever written is a sequence of these cycles.

### From Hardware to Software

Code written in Python or Java is translated into machine code — binary instructions the CPU understands — by a compiler or interpreter. An operating system sits between hardware and applications, managing all resources.

---

## EXPLORING LEVEL

### Transistors and Fabrication

A transistor is a MOSFET semiconductor device. Applying voltage to the gate terminal allows current to flow (on = 1). Removing it stops current (off = 0). Modern processors use CMOS technology, which pairs n-type and p-type MOSFETs and consumes power only when switching states.

Transistors are fabricated at the nanometre scale via photolithography. As of 2026, leading-edge processes produce gate lengths below 3 nm — approaching quantum limits.

### Von Neumann Architecture

First described by John von Neumann in his 1945 report on the EDVAC. Defining characteristic: instructions and data share the same memory space (the stored-program concept). Five logical organs: CA (ALU), CC (Control Unit), M (Memory), I (Input), O (Output).

The von Neumann bottleneck: because instructions and data share the same bus, they must be fetched sequentially. Modern CPUs mitigate this with caches, prefetching, and pipelining.

### Boolean Algebra and Logic Gates

George Boole (1854) established that logical operations could be expressed algebraically. Claude Shannon (1937) proved the same algebra describes electronic circuits. AND, OR, NOT are logically complete. NAND and NOR gates are each individually universal. From these: half adders, full adders, multiplexers, decoders, flip-flops (1-bit memory elements).

### Memory Hierarchy

| Level | Type | Speed | Capacity |
|---|---|---|---|
| L1 cache | SRAM, on-chip | ~1 ns | 32–128 KB per core |
| L2 cache | SRAM, on-chip | ~4 ns | 256 KB – 1 MB per core |
| L3 cache | SRAM, on-chip | ~12 ns | 4–128 MB shared |
| Main memory | DRAM | ~60–100 ns | 4–256 GB |
| SSD | NAND Flash | ~100 µs | 256 GB – 8 TB |
| HDD | Magnetic disk | ~5–10 ms | 1–20 TB |

Cache exploits temporal locality (programs reuse recent data) and spatial locality (programs access nearby data). A cache miss stalls the CPU while slower memory is fetched.

### Fetch-Decode-Execute in Detail

MAR ← PC (load program counter into memory address register)
MDR ← Memory[MAR] (read instruction)
IR ← MDR (load to instruction register)
PC ← PC + 1 (advance)
Decode: extract opcode and operands from IR
Execute: ALU operation, memory load/store, or branch (sets PC to new address)

Modern CPUs pipeline these stages, execute out-of-order, predict branches speculatively, and run multiple cores simultaneously.

### Instruction Set Architectures

Machine code is specific to a processor's ISA. x86-64 (Intel/AMD desktops) and ARM (phones, Apple Silicon) are incompatible. Assembly language is human-readable machine code. High-level languages are compiled or interpreted into machine code. Java compiles to bytecode for the JVM (architecture-independent virtual machine).

### COMMONLY CONFUSED

- A computer is not a calculator. A calculator is fixed-function; a computer executes any stored program.
- Memory (RAM) is not storage. RAM is fast and volatile. Storage is slow and persistent. When RAM fills, the OS uses storage as overflow (swap) — causing severe slowdowns.
- CPU clock speed is not the only performance measure. Cores, IPC, cache, and memory bandwidth all matter.
- Binary is a physical constraint, not a conceptual limitation. Any base would produce identical results on a hypothetical alternative-base machine.

---

## DEEP DIVE LEVEL

### Turing's Universal Machine

Alan Turing (1936) defined the Turing machine: an infinite tape, a read/write head, a finite state set, and a transition function. A Universal Turing Machine can simulate any other Turing machine given its description as input. This is the theoretical foundation of general-purpose computing. The Church-Turing thesis (1936, independently by Church and Turing) conjectures that any effectively computable function can be computed by a Turing machine.

### Von Neumann Architecture: Original Specification

Von Neumann (1945) defined five logical organs: CA, CC, M, I, O. The stored-program concept — instructions and data in the same memory — distinguishes the von Neumann from earlier fixed-program machines (ENIAC required physical rewiring) and from the Harvard architecture (separate instruction and data buses).

### CMOS and Moore's Law

CMOS: paired n-type and p-type MOSFETs. Static power dissipation near zero. Enabled density scaling.

Moore's Law (Gordon Moore, 1965): transistors per IC double approximately every 24 months. Held for ~60 years. Approaching limits at sub-3 nm: quantum tunnelling causes leakage. Industry responses: 3D stacking (chiplets, HBM), Gate-All-Around transistor designs, III-V semiconductor materials.

### RISC vs. CISC

RISC (Patterson & Séquin, 1981): simple instructions, fixed length, load/store architecture, single clock cycle per instruction. Examples: ARM, MIPS, RISC-V.
CISC (x86): complex multi-step instructions, variable length, many addressing modes, microcode. Example: x86-64.
In practice: modern x86 CPUs translate CISC instructions to RISC-like micro-operations internally.

### Primary Sources

- Turing, A. M. (1936). On Computable Numbers. Proc. London Math. Soc., 2nd series, 42, 230–265.
- von Neumann, J. (1945). First Draft of a Report on the EDVAC. Moore School, University of Pennsylvania.
- Shannon, C. E. (1937). A Symbolic Analysis of Relay and Switching Circuits. MIT thesis.
- Moore, G. E. (1965). Cramming more components onto integrated circuits. Electronics, 38(8).
- Patterson, D. A. & Hennessy, J. L. (2020). Computer Organization and Design. 6th ed. Morgan Kaufmann.
- Tanenbaum, A. S. & Austin, T. (2012). Structured Computer Organization. 6th ed. Pearson.

---

*The Codex Coding — thecodex.expert/coding/ — Free forever — Mumbai, India*
*Last verified: June 2026 · Source confidence: high · hello@thecodex.expert*
