Computer science | Inside the machine
How computers work: from instructions to useful results
Follow information through a computer—from bits and logic gates to processors, memory, operating systems, programs, and networks.
The big picture: hardware and software cooperate
A computer is a machine that represents information and carries out instructions. Hardware is the physical equipment: processor, memory, storage, display, keyboard, and network adapter. Software is the instructions and data that hardware uses to perform tasks.
When you click a button, the application interprets the event, asks the operating system for services, and eventually causes the processor to execute instructions. Data may be read from memory or storage, sent across a network, or displayed on a screen. The result is not produced by one mysterious component; it emerges from coordinated work.
- Input: information enters through a keyboard, sensor, file, or network.
- Processing: instructions transform data or make decisions.
- Memory and storage: values are held temporarily or retained.
- Output: results appear on a display, are saved, or are sent elsewhere.
Bits represent information
At the lowest digital level, information is represented using two states, conventionally written 0 and 1. One binary digit is a bit. A group of eight bits is a byte. Collections of bits can encode numbers, letters, images, sound, instructions, and many other kinds of information.
Binary numbers
Binary is a base-2 number system. Each position represents a power of two, just as decimal positions represent powers of ten. For example, the binary number 1101 means:
1 × 8 + 1 × 4 + 0 × 2 + 1 × 1
= 13 in decimal
With n bits, there are 2ⁿ possible bit patterns. Eight bits can represent 256 different patterns. The meaning depends on an encoding: the same pattern might be interpreted as an unsigned number, part of a character encoding, a color component, or an instruction.
Numbers and text need conventions
An unsigned 8-bit value ranges from 0 to 255. A signed 8-bit integer commonly uses two's-complement representation and ranges from -128 to 127. Arithmetic can overflow when a result cannot fit in the chosen width. Programs need to account for the language and type rules instead of assuming numbers have unlimited size.
Text is also encoded. Unicode assigns code points to characters; UTF-8 is a variable-width encoding that stores each code point using one to four bytes. A visible character can consist of multiple code points, so “one character” and “one byte” are not interchangeable concepts.
Images and sound are data too
A raster image is a grid of pixels. Each pixel stores values such as red, green, and blue intensity; more bits per color allow more possible shades. Digital audio samples a changing sound wave at regular intervals and records the measured amplitude. Resolution, sampling rate, compression, and encoding determine quality and file size.
Logic gates and circuits
Electronic circuits can implement logical operations over bits. A logic gate takes one or more binary inputs and produces an output. AND outputs 1 only when both inputs are 1; OR outputs 1 when at least one input is 1; NOT flips its input. Combining gates produces circuits for arithmetic and decision-making.
| A | B | AND | OR | A XOR B |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
An adder circuit combines logic gates to add binary numbers. A processor contains many circuits, including arithmetic logic units, registers, control logic, and interfaces to memory. Modern processors are far more complex than this simple model, but the basic principle remains: electronic state changes implement operations on encoded bits.
The processor executes instructions
The central processing unit (CPU) executes machine instructions. An instruction might add two values, compare them, move data, or branch to another instruction. The architecture defines the instruction set and the behavior programs can rely on.
Registers, instructions, and the execution cycle
Registers are small, very fast storage locations inside the CPU. A simplified instruction cycle is:
- Fetch: obtain the next instruction from memory.
- Decode: determine which operation it describes and what data it needs.
- Execute: perform the operation, often using registers or the arithmetic logic unit.
- Store or branch: save a result and select the next instruction.
Real processors use pipelines, caches, multiple execution units, and prediction to overlap work and improve throughput. Those optimizations do not change the program's intended behavior, but they make actual performance depend on factors beyond the number of source-code lines.
Programs are sequences of machine-level operations
A high-level statement such as total = price * quantity may compile to several instructions that load values, multiply them, and store the result. A compiler decides how to map source code to the processor's instruction set. The exact sequence depends on the language, compiler, optimization settings, and target machine.
Different processor architectures can use different instruction sets. A program compiled for one architecture may need to be recompiled or translated to run efficiently on another. Virtual machines and interpreters provide other ways to bridge the gap between source-level programs and hardware.
Memory, storage, and addresses
Programs need places to hold instructions and data. A computer uses a hierarchy of storage technologies with different capacities, speeds, and persistence. Fast storage close to the CPU is small and expensive per byte; larger storage is usually slower but retains information when power is off.
| Component | Typical role | Retains data without power? |
|---|---|---|
| CPU registers | Immediate operands and results | No |
| CPU cache | Recently or frequently used instructions and data | No |
| RAM | Active programs and data | No, normally volatile |
| SSD or hard drive | Files, applications, and long-term data | Yes |
| Remote storage | Data held on another computer or service | Usually, subject to service guarantees |
Addresses and program memory
Memory is organized into addressable locations. A running program uses addresses to refer to its data, but modern operating systems typically give each process a virtual address space. The operating system and hardware translate virtual addresses to physical memory and enforce separation between processes.
Program memory is commonly discussed in regions such as code, static data, the stack, and the heap. The stack holds function-call information and local values with lifetimes tied to calls. The heap supports dynamically allocated data whose lifetime is managed separately. Exact details vary by language, runtime, operating system, and architecture.
Why caches and locality matter
CPU operations are much faster than fetching data from main memory. Caches reduce this gap by keeping useful data close to the processor. Programs often run faster when they access nearby data in sequence (spatial locality) or reuse recently accessed data (temporal locality). This is one reason contiguous array traversal can perform well even when another structure has similar asymptotic complexity.
From source code to a running program
Source code is written in a programming language intended for people to read and tools to process. Depending on the language and environment, it may be compiled ahead of time, interpreted, or compiled to an intermediate form that a virtual machine executes.
- Source: the programmer writes statements and declarations.
- Translation: a compiler or interpreter checks and translates the program.
- Linking or loading: required libraries and runtime components are connected.
- Execution: the operating system starts a process and the processor runs its instructions.
- Input and output: the program requests data from files, devices, users, or networks and produces results.
A compiler commonly performs parsing, semantic checks, optimization, and code generation. A compiler can catch many errors before execution, but it cannot prove that the program matches every user expectation. Testing and careful design remain essential.
Processes and threads
A process is a running program with its own execution state and protected address space. A thread is a path of execution within a process; threads generally share the process's memory. Operating systems schedule processes and threads so multiple tasks can make progress, often by switching rapidly among them and using multiple CPU cores.
Shared memory lets threads communicate efficiently but introduces coordination problems. If two threads update the same data without appropriate synchronization, a race condition can occur: the result depends on the timing of operations. Locks, atomic operations, message passing, and careful ownership rules help manage concurrent programs.
The operating system manages resources
An operating system (OS) is the software layer that manages hardware resources and provides services to applications. It schedules work, manages memory, controls devices, organizes files, and enforces security boundaries.
- Process management: starts programs, schedules CPU time, and reports process results.
- Memory management: allocates memory and isolates processes from one another.
- File systems: organize persistent data into named files and directories.
- Device drivers: provide operating-system interfaces to hardware devices.
- System calls: let programs request privileged services such as reading a file.
Applications typically do not access hardware directly. They make requests through APIs and system calls; the operating system checks and coordinates those requests. This abstraction allows the same application concepts to work across different hardware, though platform-specific details still matter.
Files and persistence
RAM is temporary, so programs use files or databases when information must persist. A file system maps names and directories to stored data and metadata. A successful write may pass through caches before it reaches physical storage; applications with strict durability requirements need to understand the guarantees provided by their tools and storage system.
Networks move information between computers
A computer network connects devices so they can exchange data. Information is divided into units called packets, which carry payload and control information such as addressing. Routers forward packets between networks; switches connect devices within a local network.
Protocols and layers
A protocol is an agreed set of rules for communication. The Internet protocol suite divides responsibilities across layers. IP addresses and routes packets; transport protocols such as TCP provide a reliable ordered byte stream, while UDP provides a lighter datagram service without TCP's delivery guarantees. Application protocols such as HTTP define requests and responses for web services.
When a browser requests a web page, it may resolve a domain name using DNS, establish a network connection, negotiate encrypted transport using TLS, and send an HTTP request. The server processes it and returns a response. Each step can fail independently, which is why debugging networked systems involves checking names, connectivity, certificates, server behavior, and application logic.
Latency, bandwidth, and reliability
- Latency: how long it takes for information to travel or a request to receive a response.
- Bandwidth: how much data can be transferred over time.
- Throughput: the achieved rate of useful data transfer in a particular situation.
- Packet loss: data that fails to reach its destination and may need recovery.
A high-bandwidth connection can still feel slow for a small request if latency is high. Applications improve responsiveness by reducing unnecessary round trips, caching appropriate data, and handling retries carefully. Retries need limits and safe semantics; repeating a request can accidentally repeat an action such as a payment unless the operation is designed to be idempotent.
Performance, reliability, and security
A computer system's performance depends on the whole path: processor work, memory access, storage, network delays, and software design. Identify the bottleneck before optimizing. A faster CPU will not fix a service that is waiting on a distant database for every small operation.
Reliability comes from layered checks
Hardware can fail, programs can contain defects, networks can disconnect, and users can provide unexpected input. Reliable systems anticipate failure, check results, preserve data where needed, and recover in a defined way. Backups reduce the impact of data loss, but a backup is only useful if restoration has been tested.
Security is part of system design
Operating systems isolate processes and control permissions, but applications also need to validate inputs, protect credentials, use secure communication, and limit access to what is needed. A network connection does not make a service trustworthy by itself. Use established, maintained libraries and protocols; do not invent cryptography or store secrets in source code.
A compact glossary
- Bit
- A binary digit represented as one of two states, conventionally 0 or 1.
- Instruction set
- The machine-level operations supported by a processor architecture.
- Virtual memory
- An address-space abstraction that maps process addresses to physical memory and supports isolation.
- Process
- A running program with managed execution state and resources.
- Protocol
- A set of rules that communicating systems follow.
- Latency
- The delay between an action or request and its response.
Questions for further study
- How many possible values can be represented with 12 bits? What is the largest unsigned value?
- Why does information in RAM usually disappear when a computer loses power while a saved file remains?
- Trace the layers involved when a browser loads a page from a domain name.
- Why might sequentially reading an array be faster than jumping through a linked list?
- What security boundaries help prevent one process from reading another process's private memory?
References and further reading
The following books and documentation provide deeper explanations of computer architecture, operating systems, and networking.
- David A. Patterson and John L. Hennessy. Computer Organization and Design RISC-V Edition: The Hardware/Software Interface, 2nd edition. Morgan Kaufmann, 2020. Publisher information.
- Operating Systems: Three Easy Pieces. Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau. Free online book.
- Computer Networking: A Top-Down Approach. James F. Kurose and Keith W. Ross. Author resources.
- MDN Web Docs. How the web works. Introduction to web requests and responses.
Try it yourself
- Convert binary
10110to decimal by writing out each power-of-two contribution. - Draw the path a file takes from a storage device into a program that displays its contents.
- Compare latency and bandwidth with an everyday analogy, then give an example where each is the bottleneck.
- List the network steps that may happen when opening a secure website, from name lookup to response.
- Choose an everyday device and identify its inputs, processing, storage, outputs, and one likely failure mode.
Remember: computers turn encoded information into results through layers of hardware and software, each with its own job and guarantees.
Keep exploring
Try another topic
A fresh topic suggestion is ready for you.
Explore a topic