1. Home
  2. Explore
  3. Computer Science
  4. How Computers Work

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

How Computers Work

How Computers Work: From the Ground Up

These notes provide a comprehensive overview of how a modern computer is built and operates, starting from the simplest electrical circuits and building up to operating systems and user interaction. The content covers transistors, logic gates, binary arithmetic, memory, processor instructions, software concepts, and input/output devices.


Transistors: The Foundation of Digital Logic

At their core, computers are electrical circuits that operate using binary information: on or off, true or false, 1 or 0. These states correspond to whether the voltage in a circuit is high (typically 3.3 or 5 volts) or low (ground, 0 volts). The key component that enables this binary operation is the transistor, which acts as an electronically controlled switch.

Metal Oxide Semiconductor Field Effect Transistors (MOSFETs)

A MOSFET has three terminals: source, drain, and gate. The source and drain behave like the two ends of a switch, while the gate controls whether the switch is open or closed. There are two types:

  • N-channel MOSFET: Completes the circuit (closes the switch) when a high voltage is applied to the gate — analogous to a push-to-make switch.

  • P-channel MOSFET: Breaks the circuit (opens the switch) when a high voltage is applied to the gate — analogous to a push-to-break switch.

In modern computers, these two types are used together in pairs called CMOS (Complementary Metal Oxide Semiconductor). The gate terminals of a pair are always connected so they receive the same input. When a high voltage (1) is applied to the input, the N-channel MOSFET opens a path from high voltage to the output, while the P-channel closes the path to ground. When a low voltage (0) is applied, the opposite happens. By swapping the positions of the two MOSFETs, this simple circuit becomes a NOT gate (inverter): input 1 gives output 0, and input 0 gives output 1.

How Transistors Are Manufactured: Lithography

Transistors are built on a silicon substrate. Pure silicon has four valence electrons. By doping silicon with trace amounts of other elements (about one atom per million silicon atoms), regions with different electrical properties are created:

  • P-type (positive): Doped with elements having three valence electrons (e.g., gallium), creating a deficit of electrons.

  • N-type (negative): Doped with elements having five valence electrons (e.g., arsenic), creating a surplus of electrons.

Transistors are formed by sandwiching a region of one doping type between two of the other. In an N-channel MOSFET, a P-doped region sits between two N-doped regions. Without an applied electric field, this structure does not conduct. When a voltage is applied to the gate (metal contact), an electric field attracts electrons from the N-regions into the P-region, creating a conductive channel. An insulating layer (silicon oxide) separates the metal gate from the semiconductor, giving the structure its name: MOSFET (Metal Oxide Semiconductor Field Effect Transistor).

Manufacturing a processor involves building millions or billions of interconnected CMOS pairs on a single block of P-doped silicon called a die. The process uses lithography (literally "writing on stone") with two main techniques:

  1. Deposition: Chemically building up a layer of controlled thickness across the entire die (e.g., coating N-doped silicon over the P-doped die).

  2. Etching: Removing unwanted material using chemicals or hot plasma.

To target specific areas for etching, a light-sensitive chemical called a photoresist is deposited. Light is shone through a mask that exposes predetermined areas of the photoresist. The exposed regions (which must be at least as large as the wavelength of light used, hence very short-wavelength ultraviolet light) are selectively removed, then the underlying material is etched, and finally the remaining photoresist is stripped away. This process is repeated to deposit and etch the silicon oxide insulator, and then the metal contacts and interconnecting tracks (the "wires" between transistors). By repeating these steps, billions of transistors can be assembled and connected as needed.

Summary: Computers handle information as ones and zeros by representing high or low voltage. Transistors act as electronic switches controlled by voltage, always used in complementary CMOS pairs so that when one half opens the other closes. Lithography is the process of building huge numbers of interconnected transistors on a silicon block.


Boolean Algebra and Logic Gates

Transistor networks are used to implement Boolean algebra, a branch of mathematical logic dealing with binary values (true/false, 1/0). The fundamental building blocks are logic gates — circuits that take binary inputs and produce a binary output based on a logical function.

Basic Gates

  • NOT gate (inverter): Output is the opposite of the single input. If input is 1, output is 0; if input is 0, output is 1.

  • AND gate: Output is 1 only if all inputs are 1.

  • OR gate: Output is 1 if at least one input is 1.

  • XOR gate (exclusive OR): Output is 1 if the inputs are different (one is 1, the other is 0), but not both.

Complementary Gates

Each basic gate has a complementary version that is essentially the gate followed by a NOT gate:

  • NAND gate: Output is 0 only if all inputs are 1; otherwise output is 1. (AND + NOT)

  • NOR gate: Output is 1 only if all inputs are 0; otherwise output is 0. (OR + NOT)

  • XNOR gate: Output is 1 if the inputs are the same (both 0 or both 1). (XOR + NOT)

Complementary gates are denoted by the usual symbol with a small circle at the output.

Implementing Gates with CMOS

For example, a NAND gate can be created by connecting two CMOS pairs in a specific configuration. When both inputs are high (1), the N-channel transistors open a path to ground (low voltage, 0), and the P-channel transistors close the path to high voltage, so output is 0. For any other input combination, the output is 1. This matches the NAND truth table.

The NAND gate is universal: any other logic gate (AND, OR, NOT, XOR, etc.) can be constructed using only NAND gates, provided enough are available. Similarly, NOR gates are also universal. This means that once a reliable way to build NAND gates from CMOS pairs exists, all other digital logic can be implemented.

Multiple input gates are also possible — for example, a three-input AND gate can be made by connecting two two-input AND gates in series, and so on.

Summary: Logic gates are conceptual objects that make deductions from binary inputs. CMOS transistor pairs physically implement these gates in electronic circuitry. The NAND gate is universal — any other gate can be built from it.


Binary Numbers and Arithmetic

Computers represent numbers using base 2 (binary), with only two digits: 0 and 1. In base 10, digits represent multiples of powers of 10 (1, 10, 100, 1000…). In binary, digits represent multiples of powers of 2 (1, 2, 4, 8, 16…). For example, the binary number 101 means 1×4+0×2+1×1=51 \times 4 + 0 \times 2 + 1 \times 1 = 51×4+0×2+1×1=5 in base 10. Any decimal number can be represented in binary and vice versa.

Each binary digit is called a bit. Electrical tracks carry bits as high (1) or low (0) voltage. Numbers are typically handled in groups of a fixed number of bits, most commonly multiples of 8 (a byte). An nnn-bit number can represent 2n2^n2n distinct values (including zero).

Binary Addition

Adding two 1-bit numbers A and B produces a sum bit and a carry bit:

  • 0+0=00 + 0 = 00+0=0 (sum 0, carry 0)

  • 0+1=10 + 1 = 10+1=1 (sum 1, carry 0)

  • 1+0=11 + 0 = 11+0=1 (sum 1, carry 0)

  • 1+1=01 + 1 = 01+1=0 with a carry of 1 (since the result 2 in binary is 10)

The sum bit is 1 when exactly one of A or B is 1 (XOR behavior). The carry bit is 1 when both A and B are 1 (AND behavior). Therefore, a simple half adder circuit is built from one XOR gate and one AND gate. For adding multi-bit numbers, a full adder is needed that also handles a carry input from the previous lower bit. Full adders are chained together to form an adder circuit.

Binary Multiplication

Multiplying by 2 in binary is simply a left bit shift — moving each bit one place to the left and appending a 0 (just like multiplying by 10 in decimal). Multiplying by an arbitrary binary number B works by repeated addition: start with 0. If the rightmost bit of B is 1, add A. Then shift A left by one bit; if the next bit of B is 1, add this shifted A to the total. Continue for every bit of B. This is the basis of binary multiplication circuits.

Binary Comparison

To check if two numbers A and B are equal, the computer compares each corresponding pair of bits. An XNOR gate outputs 1 when its two inputs are the same. Applying XNOR gates to each bit pair, then feeding all outputs into an AND gate, gives a single bit that is 1 only if all bits match (A equals B).

Other operations (subtraction, division, greater-than comparisons) are also implemented using logic gate networks.

Signed Numbers and Floating Point

One bit of a number can be reserved to indicate the sign (0 for positive, 1 for negative). This is called a signed number. Numbers can also represent fractions using floating point representation (analogous to scientific notation in binary), though the details are more complex.

Summary: Computers represent numbers in base 2 using binary digits (bits). Logic gate networks perform arithmetic — addition, subtraction, multiplication, division — on these binary numbers. Comparison circuits check equality and other relationships.


Memory, Clock, and Storage

To build a computer, we need to decide how many bits (electrical tracks) to use for representing numbers. Commercially, this is always a multiple of 8 (byte, 16-bit, 32-bit, 64-bit). The computer has dedicated adder circuits and other arithmetic logic units (ALUs) for each supported bit width.

Registers and Latches

Arithmetic operations require storing input values and results. A latch is a circuit that holds one bit of data indefinitely once set until changed. The D-latch (data latch) is built from NAND gates with feedback: it retains its output value (0 or 1) until the enable input is activated. When enable is 1, the input bit (D) propagates to the output; when enable returns to 0, the output stays constant. Eight latches together form a register that stores one byte. Input registers hold the numbers before an operation, and an output register captures the result.

The Clock

Electrical signals take time to propagate and transistors take time to switch. To ensure operations complete before results are read, all computers have a clock — an oscillating crystal that switches voltage on and off at a precise rate (e.g., 2 gigahertz = 2 billion cycles per second). The clock synchronizes events: on one cycle, numbers load into input registers; on the next, the adder circuit works; on the third, the output register is enabled and stores the result. Most operations take multiple clock cycles; the clock ensures proper timing.

Processor: ALU and Cache

All the arithmetic logic (adders, etc.) and registers are located together on a chip called the processor or Central Processing Unit (CPU). The CPU also contains cache — small, very fast memory (tens of kilobytes) that stores intermediate calculation data for quick access.

Random Access Memory (RAM)

For larger, longer-term storage, computers have separate Random Access Memory (RAM) chips. RAM uses capacitors to store bits: a charged capacitor represents 1, a discharged one represents 0. Charge leaks over time, so memory circuits periodically refresh (top up) the charge as long as the computer is powered. Typical laptops have tens of gigabytes (billions of bytes) of RAM.

Every byte in RAM has a unique address (a binary number). The RAM contains gate networks that, when given an address, return the byte stored at that address. For many years, 32-bit addresses were sufficient (up to about 4 billion bytes). With larger RAM, 64-bit addresses are now standard — this is the difference between 32-bit and 64-bit programs and operating systems.

Hard Disk Storage

For long-term storage even without power, computers use hard disks. A hard disk is covered in tiny magnetic domains that can point up (1) or down (0). As the disk spins, a sensor head reads or flips the orientation of domains. Hard disks store hundreds of gigabytes with their own addressing scheme.

Speed vs. Size Tradeoff

There is a fundamental tradeoff between speed and storage capacity:

  • Cache (in CPU): very fast but small (kilobytes)

  • RAM: fast but larger (gigabytes), slower than cache

  • Hard disk: slow but huge (hundreds of gigabytes), persists without power

The speed of light limits how fast signals can travel — about 15 cm per clock cycle at 2 GHz. Realistically, transistor switching speeds and disk rotation delays make data access progressively slower at each level.

Summary: A clock synchronizes computer operations. Binary numbers are processed in groups of 8 bits (bytes). Registers store data during computations; cache, RAM, and hard disk provide increasing capacity at decreasing speed. Each memory location has a unique binary address.


Instructions: The Language of the Computer

A computer uses instructions to determine which operation to perform and on what data. The von Neumann architecture stores instructions (as binary numbers) in the same memory as data. For example, the binary code for instruction number 16 might mean "add two 8-bit numbers and store the result," while 17 might mean "add two 16-bit numbers." When the computer reads an instruction byte, a comparison circuit activates the appropriate logic (e.g., if byte equals 16, enable the adder circuit).

Instruction Format

Instructions and data are distinguished by their order in memory. Each type of instruction has a fixed number of bytes. An add instruction might consist of:

  • One instruction byte (e.g., 16)

  • Three memory addresses (each 8 bytes on a 64-bit system): the address of the first number, the second number, and where to store the result Total: 1+8+8+8=251 + 8 + 8 + 8 = 251+8+8+8=25 bytes.

A copy instruction might need only the instruction byte plus origin and destination addresses (17 bytes total). The next instruction always immediately follows after the previous instruction's total length. The computer starts at byte 0, interprets it as an instruction, executes it, then knows exactly where the next instruction begins based on the current instruction's length. This is how the computer sequentially processes a program.

Program Execution Example

Consider a video game where the player has a wallet with coins. Memory might contain:

  • Byte 0: add instruction byte

  • Bytes 1–24: addresses of wallet total, coins found, and wallet (to overwrite with new total)

  • Byte 25: another add instruction for selling items

  • Byte 49: next instruction, and so on

The wallet total, being frequently used, is often held in the CPU's cache as well as RAM. When the player saves the game, it is written to the hard disk.

Summary: Certain bytes in memory are interpreted as instructions that specify operations. Programs are sequences of instructions, addresses, and numbers. The computer proceeds linearly from instruction to instruction, knowing the length of each.


Turing Completeness: Jumps and Conditional Branches

Linear execution alone is not enough for general-purpose computation. Two additional instruction types make a computer Turing complete, meaning it can perform any possible computation given enough memory and time.

Jump Instruction

A jump (or go to) instruction tells the computer to continue execution at a specific memory address instead of the next sequential instruction. This enables loops. For example, the game's update loop might end with a jump back to the first instruction, causing it to repeat indefinitely.

Conditional Jump (Branch)

A conditional jump only takes effect if a certain condition is met (e.g., two numbers are equal). The comparison circuits described earlier determine the condition. With conditional jumps, the program can make decisions: different sets of instructions are executed based on data or input (keyboard, mouse). This is what makes a computer Turing complete — it can branch based on results, enabling algorithms like "if the player presses 'B', buy an item; otherwise, continue."

High-Level Programming Languages

Writing instructions in raw binary (machine code) is extremely difficult. Programmers use high-level languages (like C, C++, Python, Java) that are human-readable. For example:

if (A > B) {
    Q = A;
} else {
    Q = B;
}

This sets Q to the larger of A and B. A compiler translates this high-level code into the correct sequence of instructions, memory addresses, jumps, and bit widths. A compiled program file contains the binary instructions the computer can execute directly.

Functions and Libraries

A function (or method) is a reusable block of code that performs a specific task (e.g., square root, fetch email). The instructions for a function are stored in memory once. When the function is called, the computer jumps to its location, executes the instructions, and jumps back to the point where it was called. Functions can be compiled into libraries (e.g., DLL files on Windows) for sharing across programs.

Summary: Jumps allow loops; conditional jumps enable decision-making. Any computer with these capabilities is Turing complete. High-level languages and compilers simplify programming, and functions/libraries promote code reuse.


Input and Output: Graphics, Sound, and User Input

Computer Displays

Displays consist of a grid of pixels, each pixel made of three tiny light sources: one red, one green, one blue (RGB). By varying the brightness of each subpixel from 0 to 255 (the range of a byte), any color can be created. For example:

  • Red + green = yellow/brown

  • Red + blue = pink

  • All three at full = white

  • All three at partial = grey

A display might have 1920 × 1080 pixels (Full HD). To store one frame:

3 bytes/pixel×1920×1080=6,220,800 bytes3 \text{ bytes/pixel} \times 1920 \times 1080 = 6,220,800 \text{ bytes}3 bytes/pixel×1920×1080=6,220,800 bytes

The computer updates this memory block 30–60 times per second and sends the data to the display.

Pixel positions correspond to coordinates in memory. If the display is 1000 pixels wide and each pixel takes 3 bytes, moving 3000 bytes forward moves to the rightmost pixel of the current row; the next pixel is the first pixel of the next row. Algorithms draw windows, buttons, text, and graphics using arithmetic on these coordinates. For example, the Bresenham line-drawing algorithm uses a loop to draw a straight line between two (x,y)(x, y)(x,y) points. Three lines form a triangle; thousands of textured, illuminated triangles create 3D shapes. All graphics ultimately reduce to addition, multiplication, and jump instructions.

Sound

Sound is created by moving a speaker membrane back and forth, generating pressure waves (sound waves). A speaker has a permanent magnet attached to a membrane inside a fixed conducting coil. An oscillating electric current through the coil pulls the magnet in and out, moving the membrane. The computer breaks time into discrete steps and sends a binary number representing the desired current at each step to a sound card, which converts it to an analog voltage. Microphones work in the reverse direction.

Input Devices

Input devices (keyboard, mouse, game controller) send messages to the computer — groups of bytes in memory identifying the device and the action. For example, a message queue might contain:

  • Key 'A' pressed (key code 65)

  • Mouse moved 30 pixels right

  • Shift key released

Keyboard

Keys are arranged in a grid. Pressing a key creates an electrical contact between a horizontal and a vertical row, uniquely identifying the key. The keyboard relays a message (key down or key up) to the computer's memory. In Windows, the 'B' key corresponds to decimal 66; the game might use a conditional jump: if last keystroke == 66, jump to "buy" routine.

Mouse

A mouse tracks horizontal and vertical movement over a flat surface, plus button clicks. The operating system maintains (x,y)(x, y)(x,y) pixel coordinates of the cursor by adding/subtracting movement deltas. When a left button click occurs, the program checks the cursor coordinates against each on-screen button's rectangle (left, right, top, bottom boundaries). If the xxx-coordinate is between the button's left and right edges AND the yyy-coordinate is between the top and bottom, then the button is considered pressed. This check repeats for every button.

Summary: Displays are grids of RGB pixels, each controlled by bytes in memory. Sound is produced by converting binary values to oscillating currents that move a speaker membrane. Input devices send messages to memory; the computer interprets key codes and mouse positions using conditional logic to respond.


Final Overview: Building Complexity from Simplicity

Computing is about layering simpler concepts to achieve complex results. Starting from the ground up:

  • Transistors (CMOS pairs) → can make a NAND gate

  • NAND gates → can make any logic gate (universal)

  • Logic gates → can make adders, comparators, latches

  • Arithmetic and storage → can make instructions

  • Instructions → can make algorithms, functions, programs

  • Programs → can build operating systems, games, browsers of limitless complexity

Despite their apparent sophistication, computers are fundamentally dumb: they have no intuition and must rigidly follow programs that spell out every possible situation in exhaustive detail. The entire digital world arises from doping silicon with impurities and performing relatively simple logical operations at incredible speed and scale.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent