Bitpath

How Computers Work

See what really happens when your code runs: how numbers become bits, where data lives in memory, how the CPU steps through instructions, and how the operating system shares one machine between many programs. Every idea is traced by hand and checked in Python.

  • Beginner
  • 4 units, 12 lessons, 153 exercises
  • Python, a little C, diagrams
  • Builds on basic programming

Unit 01 · 3 lessons

Bits and numbers

Read the bit patterns behind integers, hex and floats

  1. > start01Counting in two
  2. 02Whole numbers
  3. BOSSWhy 0.1 + 0.2 isn't 0.3
Unit prize
  1. 01
    Counting in two

    Bits, bytes, binary and hex

  2. 02
    Whole numbers

    Fixed widths, overflow, negatives and bit operators

  3. BOSS
    Why 0.1 + 0.2 isn't 0.3

    How fractions are stored, and how to live with it

Read the unit guide

A bit is one switch: 0 or 1. Everything a computer stores is a row of bits, and n bits can show 2ⁿ different patterns. Eight bits make a byte: 256 patterns, read as the numbers 0 to 255.

Binary works like decimal with places worth 1, 2, 4, 8 and so on instead of 1, 10, 100. Hex is shorthand for it: one hex digit (0-9, then a-f) stands for exactly four bits, so a byte is two hex digits. In Python, 0b1010 and 0xa are both ten, and bin() and hex() go the other way.

Most languages give integers a fixed width: 8, 16, 32 or 64 bits. Count past the top and the number wraps around to the bottom, like an odometer. Signed integers use two's complement, where the top bit is worth minus its usual value, so 8 bits cover -128 to 127. Python's own int is the exception: it grows as needed and never wraps.

The bit operators work on every bit at once. & keeps bits set in both numbers, | in either, ^ in exactly one. << shifts left (doubling), >> shifts right (halving). x & 0xFF keeps the lowest byte.

A float is scientific notation in binary: 64 bits split into a sign, an exponent and a 53-bit fraction. Only sums of halves, quarters, eighths and so on are exact, so 0.1 is stored as the nearest value it can reach. That is why 0.1 + 0.2 == 0.3 is False.

So: compare floats with a tolerance, never with ==. Keep money in whole cents. And remember that above 2⁵³ a float can no longer hold every whole number.

Unit 02 · 3 lessons

Memory

Know where your data lives and why its layout decides speed

  1. 01Addresses and pointers
  2. 02Stack and heap
  3. BOSSArrays versus linked lists, for real
Unit prize
  1. 01
    Addresses and pointers

    Memory is a numbered row of bytes

  2. 02
    Stack and heap

    Two regions, two lifetimes

  3. BOSS
    Arrays versus linked lists, for real

    Why the cache decides which O(n) is fast

Read the unit guide

Memory (RAM) is one long row of bytes, each with a number called its address. A value that takes several bytes sits in neighbouring addresses. A pointer, or reference, is simply an address kept as a value: it says where something else lives.

Arrays put their elements side by side, so element i is at start + i × size. That one multiplication is why indexing is O(1). In Python every name holds a reference to an object, so b = a makes a second name for the same list, not a second list.

A running program splits memory in two. The stack holds one frame per active function call (its parameters and locals), pushed on call and popped on return, which makes it very fast but short-lived. The heap holds everything that must outlive the call that made it, at the price of someone having to free it later.

Who frees heap memory depends on the language: you, by hand, in C; a reference count plus a garbage collector in Python; a garbage collector in Java, JavaScript and Go. A collector only frees what nothing refers to any more, so a forgotten reference in a long-lived list is still a leak.

RAM is about a hundred times slower than the CPU's small caches, and a cache miss fetches a whole 64-byte line. Code that walks memory in order gets the neighbours for free; code that hops between scattered heap nodes pays a miss for nearly every step.

So two O(n) loops can differ several times over in real speed. When speed matters, prefer contiguous arrays, walk them in order, and keep the data you use together close together.

Unit 03 · 3 lessons

The processor

Follow how a CPU runs instructions, and what makes it fast or slow

  1. 01Fetch, decode, execute
  2. 02From source to machine
  3. BOSSThe sorted-array mystery
Unit prize
  1. 01
    Fetch, decode, execute

    The one loop every CPU runs

  2. 02
    From source to machine

    Compilers, interpreters, bytecode and JITs

  3. BOSS
    The sorted-array mystery

    Pipelines, branch prediction and a famous puzzle

Read the unit guide

A CPU does one thing, billions of times a second: fetch the instruction at the address in its program counter (PC), decode it, execute it, and move the PC on. Its working values sit in a handful of tiny, fast slots called registers.

Instructions are very small: load a value, add two registers, compare, jump. A jump overwrites the PC. Jumping backward makes a loop, and a conditional jump (jump only if some value is zero, say) makes an if. Every loop and branch you write becomes jumps.

Your source code is text, so something must translate it. A compiler (C, Rust, Go) turns the whole program into machine code before it runs. An interpreter (Python) is itself a program that reads your code, usually as compact bytecode, and carries it out step by step. A JIT (the V8 engine in Node and Chrome) starts by interpreting and compiles the hot parts to machine code while the program runs. tsc only turns TypeScript into JavaScript: the types are gone before anything runs.

Interpreting costs dozens of machine instructions for each small step of your program, which is why a tight loop is far slower in pure Python than in C, and why pushing the loop into a built-in or NumPy helps so much.

To go faster, CPUs overlap instructions in a pipeline: while one executes, the next is already being decoded. That forces the CPU to guess which way each branch will go. A right guess is free. A wrong one throws away the half-done work and costs around 15 cycles.

So predictable branches are cheap and random ones are expensive: the same loop can run several times faster on sorted data. Clock speeds stopped climbing in the mid-2000s, so chips grew more cores instead, and extra cores only help work that can be split.

Unit 04 · 3 lessons

The operating system

See how one machine is shared safely between many programs

  1. 01Processes
  2. 02System calls
  3. BOSSMore memory than you have
Unit prize
  1. 01
    Processes

    Running programs and how they take turns

  2. 02
    System calls

    How programs ask the kernel for help

  3. BOSS
    More memory than you have

    Virtual addresses, pages, faults and swap

Read the unit guide

The operating system is the program that runs the programs. A program is a file on disk; a process is a running copy of it, with a process ID (PID), its own memory, and its own saved registers and program counter. Start the same program twice and you get two processes that share nothing but the file.

There are far more processes than cores, so the scheduler gives each runnable process a short time slice, a few milliseconds, then a timer interrupt hands control back to the OS, which picks the next. Saving one process's state and restoring another's is a context switch. Processes waiting for a disk, the network or a key press are blocked and need no turn at all, which is why hundreds of processes can share a few cores.

Programs run in user mode, where the CPU refuses direct access to hardware. To read a file, send a packet or even ask the time, a program makes a system call: the CPU switches to kernel mode, the kernel checks the request, does the work and switches back. Open files are named by small numbers called file descriptors: 0, 1 and 2 are standard input, output and error.

A system call costs far more than a function call, so libraries buffer: they collect many small writes and hand them to the kernel in one call. The price is that buffered data is lost if the program dies before a flush.

Every process sees its own private row of addresses, its virtual memory. The CPU translates each address through the process's page table, one 4 KB page at a time, to wherever the kernel put that page in physical RAM. That is how two processes can both use address 0x1000 without touching each other.

Pages get real memory only when first touched, and pages that do not fit can be moved out to swap on disk. Touching a page that is not in RAM is a page fault: the kernel quietly loads it, at a cost of around a thousand RAM accesses. A program that faults all the time is thrashing; one that touches more than RAM and swap can hold gets killed.

Learn How Computers Work on your phone.

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy