Bytebook Compiler

Computer Basics

How Computers Work

What is inside a computer — CPU, memory, storage and I/O — the fetch–decode–execute cycle, the memory hierarchy, and what an operating system does for your programs.

4 min read4-question quiz

Every program you will write in C, Python or C++ eventually runs on the same few pieces of hardware. Knowing them explains why some code is fast and some is slow.

The main parts

Part Job Analogy
CPU (processor) executes instructions: arithmetic, comparisons, jumps the chef
RAM (main memory) holds running programs and their data; fast but volatile (erased when power goes off) the kitchen counter
Storage (SSD/HDD) keeps files permanently; much slower than RAM the pantry
Input devices keyboard, mouse, camera, microphone, network orders coming in
Output devices screen, speakers, printer, network plates going out
Motherboard / bus connects everything and carries data between parts the corridors

This design — a CPU that fetches both instructions and data from the same memory — is the von Neumann architecture (1945), and nearly every computer still follows it.

Inside the CPU

  • ALU (arithmetic logic unit): adds, subtracts, compares, does AND/OR/NOT.
  • Control unit: decodes instructions and directs the other parts.
  • Registers: a few tiny, ultra-fast storage slots inside the CPU, including the program counter (address of the next instruction).
  • Cache: small fast memory between the CPU and RAM (L1, L2, L3).
  • Clock: a 3 GHz CPU ticks 3 billion times per second. Modern chips have several cores, each able to run its own stream of instructions.

The fetch–decode–execute cycle

The CPU repeats three steps billions of times per second:

Power onFETCH the instruction at the program counterDECODE it: which operation, which data?EXECUTE it in the ALU, read or write memoryMove the program counter to the next instructionRepeat (billions of times per second)
The instruction cycle. A loop in your code is just the CPU jumping the program counter back to an earlier instruction.

The memory hierarchy

Faster memory is more expensive and smaller. Computers stack several layers:

Level Typical size Typical access time
Registers under 1 KB under 1 ns
L1 cache 32–64 KB per core about 1 ns
L2 / L3 cache 1–64 MB 3–15 ns
RAM 8–32 GB about 100 ns
SSD 256 GB–2 TB about 100,000 ns (0.1 ms)
Hard disk 1–10 TB about 10,000,000 ns (10 ms)

Reading from RAM is about 100 times slower than from L1 cache. Programs that access memory sequentially (like walking through an array) let the cache prefetch data and run much faster than programs that jump around (like following linked-list pointers).

Units of data

Unit Size
bit a single 0 or 1
byte 8 bits — one character in ASCII
kilobyte (KB) 1,024 bytes (strictly, KiB)
megabyte (MB) 1,024 KB
gigabyte (GB) 1,024 MB
terabyte (TB) 1,024 GB

(Storage makers use powers of 1000, which is why a "1 TB" drive shows up as about 931 GB.)

The operating system

The operating system (Linux, Windows, macOS, Android) is the program that manages the hardware for every other program:

  • Process management — runs many programs at once by switching the CPU between them very quickly.
  • Memory management — gives each program its own memory space and protects it from others.
  • File system — organises storage into files and folders.
  • Device drivers — talk to hardware like printers, Wi-Fi and cameras.
  • User interface — a command line (shell) or a graphical desktop.
  • Security — users, permissions and passwords.

When your C program calls printf or fopen, the C library asks the operating system to do the actual work through a system call.

Software layers

BrowserVS CodeYour programsApplicationsKernelDriversOperating systemCompilers & toolsSystem softwareYou
Software sits in layers: applications rely on system software, which controls the hardware.

Test yourself

4 questions · pick an answer to check it instantly

  1. 1

    Which part of the computer loses its contents when power is switched off?

  2. 2

    What are the three steps of the instruction cycle?

  3. 3

    Which is fastest to access?

  4. 4

    Which job belongs to the operating system?