How computers transform photographs, music, words, and programs into patterns of two symbols — and what happens when that transformation is imperfect
Chapter 4 introduces one of the most important and surprising ideas in computer science: all digital information — every photograph, every song, every word, every number, every video, every program — is stored and processed as patterns of just two symbols. This chapter builds patient, visual understanding of binary representation. Students learn bits, bytes, binary counting, decimal-to-binary conversion, text encoding, image representation, sound representation, compression, and the limits of digital representation.
By the end of the chapter, you should understand why representation matters, how computers transform the world into numbers, and what happens when that transformation is imperfect. These ideas sit beneath every file, every format, every network transmission, and every hardware design in computing.
Your phone holds thousands of photographs, hundreds of songs, years of text messages, and every app you have installed — all stored on a chip the size of a fingernail. How? Every one of those photographs, songs, words, and programs has been translated into a long sequence of two symbols: 0 and 1. By the end of this chapter, you will understand exactly how that translation works, why two symbols are sufficient for everything, and what is gained and lost in the process.
Before a computer can store, process, or transmit anything — a name, a temperature reading, a photograph, a piece of music — it must answer a fundamental question: how do we turn this into something the machine can handle?
That process of turning information into a form a machine can work with is called representation. Every choice about representation has consequences. Some choices make storage smaller. Some make processing faster. Some lose information in the translation. Some preserve everything exactly. Some work for certain kinds of information and fail for others.
Consider something simple: your name. Your name is a sequence of letters. But a computer does not know what a letter is. It only knows electrical signals — voltage present or voltage absent, current flowing or current not flowing, a magnetic domain pointing one way or another. To store your name, a computer needs a way to connect the human idea of a letter to a pattern of electrical states that the machine can record and retrieve.
That connection — from human meaning to machine-readable pattern and back again — is what representation systems provide.
One of the central choices in digital computing is to reduce everything to numbers, and then to represent those numbers in binary form. Text becomes numbers. Colors become numbers. Musical tones become numbers. Video frames become numbers. Even programs themselves — the instructions that tell the computer what to do — are stored as numbers.
This is a remarkable commitment. It means the same physical hardware that stores a piece of music can also store a photograph or a legal document. The price of this generality is that the computer itself does not know what kind of information it is working with. A file full of numbers might be music or it might be a spreadsheet. The computer relies on software, file formats, and encoding standards to tell it what the numbers mean.
| Why Representation Matters | The Question It Answers |
|---|---|
| File sizes vary | A song is larger than a text document because audio requires far more numbers to represent. |
| Images look blocky | Compression systems discard information to save space; when too much is discarded the loss becomes visible. |
| Text displays incorrectly | If a program uses the wrong encoding, numbers are matched to the wrong characters. |
| Some calculations are slightly wrong | Computers store decimal fractions as binary approximations, which introduce tiny rounding errors. |
| Storage is measured in specific units | Bits, bytes, kilobytes, megabytes, gigabytes all reflect the binary foundation of digital storage. |
The smallest unit of information in a digital computer is called a bit, short for binary digit. A bit can hold exactly one of two values: 0 or 1. By itself a bit is almost nothing. But bits are never used alone — they are organized into groups, and in groups they become enormously expressive.
Hard disk drive — A tiny magnetic region on a spinning disk, magnetized in one of two directions.
Solid-state drive (SSD) — A charge level stored in a flash memory cell.
RAM — A flip-flop circuit that can be set to either of two stable states.
Fiber-optic cable — A pulse of light present (1) or absent (0).
Eight bits grouped together form a byte. The byte is the standard working unit for most computing purposes. Eight bits can represent 2⁸ = 256 different values — enough for all characters in simple text encoding systems. As computers handle more data, storage is measured in progressively larger units.
| Unit | Abbreviation | Approximate Size |
|---|---|---|
| Bit | b | 1 binary digit |
| Byte | B | 8 bits |
| Kilobyte | KB | ~1,000 bytes (exactly 1,024 in binary) |
| Megabyte | MB | ~1,000,000 bytes |
| Gigabyte | GB | ~1,000,000,000 bytes |
| Terabyte | TB | ~1,000,000,000,000 bytes |
| Petabyte | PB | ~1,000,000,000,000,000 bytes |
Internet speeds are advertised in bits per second (Mbps or Gbps). File sizes are measured in bytes (MB or GB). A 100 Mbps connection transfers about 12.5 MB per second, because there are 8 bits per byte. Always check whether a unit is bits (lowercase b) or bytes (uppercase B).
Computers use binary — the base-2 number system — because it maps perfectly onto two-state physical hardware. The decimal system we use in everyday life is base-10: it uses ten symbols (0–9) and groups quantities in powers of ten. Binary uses base-2: it uses only two symbols (0 and 1) and groups quantities in powers of two. The logic is identical; only the base changes.
| Thousands (10³) | Hundreds (10²) | Tens (10¹) | Ones (10⁰) | Total |
|---|---|---|---|---|
| 3 | 4 | 7 | 2 | 3,000 + 400 + 70 + 2 = 3,472 |
Each position in a binary number represents a power of 2. The digit in that position tells you how many of that power's worth to count — either 0 or 1.
Position: 7 6 5 4 3 2 1 0 Power: 2⁷ 2⁶ 2⁵ 2⁴ 2³ 2² 2¹ 2⁰ Value: 128 64 32 16 8 4 2 1
| 8 (2³) | 4 (2²) | 2 (2¹) | 1 (2⁰) |
| 1 | 0 | 1 | 1 |
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
| Decimal | Binary | Decimal | Binary |
|---|---|---|---|
| 0 | 0000 | 8 | 1000 |
| 1 | 0001 | 9 | 1001 |
| 2 | 0010 | 10 | 1010 |
| 3 | 0011 | 11 | 1011 |
| 4 | 0100 | 12 | 1100 |
| 5 | 0101 | 13 | 1101 |
| 6 | 0110 | 14 | 1110 |
| 7 | 0111 | 15 | 1111 |
Pattern 1 — The rightmost digit alternates with every step: 0, 1, 0, 1 ...
Pattern 2 — Each column changes half as often as the column to its right. The twos place changes every 2 counts. The fours place every 4. The eights place every 8.
Pattern 3 — The maximum value of n bits is 2ⁿ − 1. Four bits: 0 to 15. Eight bits: 0 to 255. Sixteen bits: 0 to 65,535.
Pattern 4 — Adding one more bit doubles the number of representable values: 4 bits → 16 values; 5 bits → 32 values.
0101 (decimal 5)
+ 0011 (decimal 3)
------
1000 (decimal 8) ✓Converting by hand builds intuition that a calculator cannot provide. Binary-to-decimal conversion uses the place-value table from Section 4.3. For decimal-to-binary, the most reliable method is repeated division by 2.
Divide the number by 2 repeatedly, collecting remainders. Read the remainders from bottom to top to obtain the binary representation.
25 ÷ 2 = 12 remainder 1
12 ÷ 2 = 6 remainder 0
6 ÷ 2 = 3 remainder 0
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1
200 ÷ 2 = 100 remainder 0
100 ÷ 2 = 50 remainder 0
50 ÷ 2 = 25 remainder 0
25 ÷ 2 = 12 remainder 1
12 ÷ 2 = 6 remainder 0
6 ÷ 2 = 3 remainder 0
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1
| Decimal | Binary | Notes |
|---|---|---|
| 1 | 0000 0001 | |
| 2 | 0000 0010 | |
| 4 | 0000 0100 | |
| 8 | 0000 1000 | |
| 16 | 0001 0000 | |
| 32 | 0010 0000 | |
| 64 | 0100 0000 | |
| 128 | 1000 0000 | |
| 255 | 1111 1111 | Maximum 8-bit value |
1100 1010?0001 1111?0011 1010 + 0001 0110 in binary. Give result in both binary and decimal.Text is made of characters: letters, digits, punctuation, spaces, and special symbols. A computer stores only numbers. The solution is a character encoding — a standardized table that maps characters to numbers. If both the program that writes the text and the program that reads the text use the same encoding table, the text displays correctly. If they use different tables, the characters appear wrong.
The earliest widely-used encoding for English text is ASCII (American Standard Code for Information Interchange), developed in the early 1960s. ASCII assigns numbers from 0 to 127 to 128 characters: control codes, printable characters, digits, punctuation, and both cases of all 26 English letters.
| Character | Decimal | Binary | Character | Decimal | Binary |
|---|---|---|---|---|---|
| Space | 32 | 0010 0000 | a | 97 | 0110 0001 |
| ! | 33 | 0010 0001 | b | 98 | 0110 0010 |
| 0 | 48 | 0011 0000 | z | 122 | 0111 1010 |
| 9 | 57 | 0011 1001 | A | 65 | 0100 0001 |
| B | 66 | 0100 0010 | Z | 90 | 0101 1010 |
Uppercase A is 65; lowercase a is 97. The difference is exactly 32 for every letter. This is intentional — it makes case conversion simple for programs. The bit at position 5 (value 32) is the only difference between a capital letter and its lowercase version.
H → 0100 1000
i → 0110 1001
! → 0010 0001
ASCII was designed for American English. With only 128 values, it has no room for accented letters (é, ñ, ü), non-Latin alphabets (Arabic, Chinese, Cyrillic, Hindi), mathematical symbols, or emoji. As computing became global, this limitation became a serious problem.
Unicode is a modern standard that assigns a unique number (called a code point) to every character in virtually every writing system on Earth — and to emoji, mathematical symbols, musical notation, and much more. As of recent editions, Unicode covers over 140,000 characters across 150+ scripts.
UTF-8 is the most common way of encoding Unicode text as bytes. It is backward-compatible with ASCII: the first 128 Unicode code points use the same numbers as ASCII and store in exactly one byte each. Characters outside the ASCII range use 2, 3, or 4 bytes. This is why most English text files remain small and fast to process in UTF-8 — they use one byte per character — while Chinese or Arabic text uses more bytes per character.
UTF-8 is the dominant encoding for the web, for most programming languages, and for most modern software. Encoding mismatches produce garbled text — question marks, boxes, or strange characters where letters should appear.
A digital image is made of pixels — tiny colored squares arranged in a rectangular grid. (Pixel is short for "picture element.") If you zoom into a digital photograph far enough, you will see the individual pixels clearly. What looks like a smooth curve in a photograph is actually many tiny squares of slightly different colors arranged to approximate that curve.
An image's resolution is the number of pixels in its grid, expressed as width × height. A common display resolution is 1920 × 1080 pixels. A high-resolution smartphone photograph might be 4000 × 3000 pixels or larger.
The most common system for representing color digitally is RGB — Red, Green, Blue. Every pixel's color is described as a mixture of three components, each stored as an 8-bit number (0 to 255). This gives 256 × 256 × 256 = 16,777,216 possible colors per pixel. Three bytes per pixel is called 24-bit color or true color.
Each pixel stores three values:
RED GREEN BLUE
[0 to 255] [0 to 255] [0 to 255]
| | |
└─────────────┴────────────┘
|
= 1 color per pixel
Examples:
(255, 0, 0) → Pure red
( 0, 255, 0) → Pure green
( 0, 0, 255) → Pure blue
(255, 255, 0) → Yellow (red + green)
(255, 165, 0) → Orange
( 0, 0, 0) → Black
(255, 255, 255) → White
(128, 128, 128) → Medium gray
| Color | Red | Green | Blue |
|---|---|---|---|
| Pure red | 255 | 0 | 0 |
| Pure green | 0 | 255 | 0 |
| Pure blue | 0 | 0 | 255 |
| White | 255 | 255 | 255 |
| Black | 0 | 0 | 0 |
| Yellow | 255 | 255 | 0 |
| Orange | 255 | 165 | 0 |
| 50% gray | 128 | 128 | 128 |
File size = width × height × 3 bytes1920 × 1080 × 3 = 6,220,800 bytes ≈ 6.2 MB
12,000,000 × 3 = 36,000,000 bytes = 36 MB (uncompressed)
Some formats store a fourth value per pixel — alpha — representing transparency. Alpha 0 = fully transparent; alpha 255 = fully opaque. PNG supports alpha channels, which is why it is used for logos and graphics with transparent backgrounds. JPEG does not support transparency, which is why JPEG images always have a solid background.
Sound is vibrations traveling through air, creating pressure waves that our ears detect. A microphone converts those pressure waves into a continuous electrical signal — a changing voltage that mirrors the shape of the sound wave. A continuous signal is called an analog signal. To store sound digitally, we must convert that continuous analog signal into a sequence of discrete numbers. This process is called analog-to-digital conversion (ADC).
The key technique is sampling: measuring the amplitude (height) of the sound wave at regular time intervals and storing each measurement as a number. The sampling rate is how many samples are taken per second, measured in hertz (Hz) or kilohertz (kHz). The bit depth is how many bits store each sample — more bits mean more possible amplitude levels and finer detail.
ANALOG SOUND WAVE
│ /\ /\ /\
Amp│ / \ / \ / \ /\
│ / \ / \ / \ / \ /\
──│──/──────\/──────\────/──────\──/────\──/──\──
│ \ / \/ \/ \/
└──────────────────────────────────────────────
Time →
SAMPLING — measure amplitude at regular intervals
↓ ↓ ↓ ↓
Value: 42 180 255 201 87 ...
STORED AS NUMBERS: 42, 180, 255, 201, 87 ...
PLAYBACK: Numbers → DAC → Reconstructed wave → Speaker → Sound
| Use Case | Sampling Rate | Bit Depth | Quality |
|---|---|---|---|
| Telephone | 8,000 Hz | 8-bit | Voice intelligibility only |
| CD audio | 44,100 Hz | 16-bit | Full human hearing range |
| Professional audio | 48,000–96,000 Hz | 24-bit | Studio/film quality |
Size = sampling rate × (bit depth ÷ 8) × channels × seconds
44,100 × (16 ÷ 8) × 2 × 60
= 44,100 × 2 × 2 × 60
= 10,584,000 bytes
≈ 10.6 MB per minute
To accurately capture a sound frequency, the sampling rate must be at least twice that frequency. Human hearing extends to roughly 20,000 Hz. CD audio samples at 44,100 Hz — more than twice 20,000 — which is why CD audio captures everything the human ear can hear.
Representing images, audio, and video using the methods described above produces very large files. A one-hour uncompressed HD video might require 500 GB or more. Compression reduces file size by finding smarter ways to represent data — using fewer bits without losing important information, or while accepting controlled losses.
Lossless compression shrinks a file so that when it is decompressed, the result is bit-for-bit identical to the original. Run-length encoding is one of the simplest techniques: instead of repeating the same value many times, the compressed file stores the value once with a count.
(white, 40) (black, 12) (white, 48)
Other lossless methods include dictionary compression (used in ZIP files), which finds repeated patterns and replaces them with short codes, and Huffman coding, which assigns shorter bit sequences to more frequent values. PNG images and ZIP files both use lossless compression. Text compression is always lossless — you cannot afford to lose any characters from a document.
Lossy compression achieves much higher compression ratios by discarding some information permanently. The decompressed file is not identical to the original — but if the compression is well-designed, the differences are difficult or impossible for humans to perceive.
JPEG exploits the fact that the human visual system is more sensitive to brightness than to color, and more sensitive to large-scale patterns than to fine high-frequency detail. It divides the image into 8×8 blocks, applies a mathematical transform to each block, rounds away the high-frequency detail, and compresses the result. At high quality settings, losses are nearly invisible. At low quality, the 8×8 block structure becomes visible — the "blockiness" or "JPEG artifacts" you may have noticed in heavily compressed images.
MP3 uses a similar strategy for audio, discarding sounds masked by louder simultaneous sounds, very quiet sounds, and sounds at the extremes of the audible frequency range.
ORIGINAL DATA
│
├──→ LOSSLESS ──→ Compressed file
│ │ │
│ └──────────── DECOMPRESS ──→ Exact original restored
│
└──→ LOSSY ──→ Compressed file (much smaller)
│ │
└──────────── DECOMPRESS ──→ Approximate original
(some detail permanently lost)
| Factor | Lossless | Lossy |
|---|---|---|
| Information preserved | 100% | Some lost permanently |
| Typical compression ratio | Modest | High |
| Suitable for text/code | Yes | Never |
| Suitable for photographs | Yes (PNG, TIFF) | Yes (JPEG), if quality acceptable |
| Suitable for audio | Yes (FLAC) | Yes (MP3), if quality acceptable |
| Can compress further? | Yes | Yes, but quality degrades further |
A file that has already been lossy-compressed will lose additional quality if compressed again at lower quality. Repeatedly saving JPEG images or re-encoding MP3 files degrades their quality each time, because each pass discards more information permanently.
Digital representation is powerful, but it is not perfect. Every representation system has limits, and understanding those limits is part of working carefully with computers.
Each numeric type has a maximum value. If a calculation tries to exceed that maximum, the result wraps around or triggers an error — a problem called overflow. An 8-bit unsigned integer can hold values from 0 to 255. If you add 1 to 255 in a system without overflow protection, the result is 0, not 256.
255 = 1111 1111 (8 bits)
+ 1 = 0000 0001
─────────────────
256 = 1 0000 0000 ← 9 bits
Stored: 0000 0000 = 0
The Ariane 5 rocket was destroyed shortly after launch because of an overflow error. A value that was safely within range in the Ariane 4 software exceeded the representable range in the Ariane 5's different hardware, causing the flight computer to crash and the rocket to self-destruct. Overflow errors have real consequences.
Computers typically represent real numbers using floating-point representation. Floating-point numbers are stored in binary, which means they can represent fractions whose denominators are powers of 2 — like 1/2, 1/4, 1/8 — exactly. But fractions whose denominators are not powers of 2 — like 1/10 — cannot be stored exactly in binary, just as 1/3 cannot be stored exactly in decimal.
0.1 + 0.2 should equal 0.30.1 + 0.2 = 0.300000000000000040.1 + 0.2 == 0.3 may evaluate to False in most programming languages.Sampling limits: Digital audio captures sound at a finite number of samples per second. Frequencies above half the sampling rate cannot be represented accurately — they produce aliasing, where high-frequency content appears at incorrect lower frequencies. Similarly, fine image detail smaller than one pixel is simply lost.
Encoding errors: If a file is saved with one encoding (say, UTF-8) but opened by software expecting a different encoding (say, Windows-1252), the bytes are matched to the wrong characters, producing garbled text. Text files should always be labeled with their encoding.
Lossy compression artifacts: Once an image has been saved as JPEG at low quality, the discarded detail cannot be recovered. Medical imaging, scientific photography, and archival records should not use lossy compression.
| Strength of Digital Representation | Corresponding Limitation |
|---|---|
| Exact storage of values within range | Cannot represent values outside the range (overflow) |
| Perfect copying and transmission | Lossy compression permanently discards information |
| Consistent encoding standards | Encoding mismatches cause garbled text |
| High precision with many bits | Floating-point introduces tiny approximation errors |
| Compression reduces storage enormously | Compression artifacts can degrade quality |
Position: 7 6 5 4 3 2 1 0
Power: 2⁷ 2⁶ 2⁵ 2⁴ 2³ 2² 2¹ 2⁰
Value: 128 64 32 16 8 4 2 1
To read binary → sum values at positions containing 1
To write binary → find powers of 2 that sum to target (subtraction method)
OR divide by 2 repeatedly, read remainders upward
TEXT → Character encoding (ASCII / UTF-8)
Each character mapped to a number → stored as bytes
IMAGE → Grid of pixels × RGB bytes per pixel
Uncompressed: width × height × 3 bytes
JPEG: lossy compression removes high-frequency detail
PNG: lossless compression, supports transparency
AUDIO → Sampling at regular intervals
File size = rate × (depth ÷ 8) × channels × seconds
MP3: lossy, ~1 MB/min FLAC: lossless, ~30–35 MB/min
PROGRAM → Machine instructions encoded as numbers
Same binary format as everything else
Answer in your own words before attempting the longer practice questions.
1100 1010?0001 1111?0011 1010 + 0001 0110 in binary; give result in binary and decimal.Each problem contains an error. Find it, explain it, and correct it.
What is the correct approach? What is the actual decimal value?
What rule did the student get backwards? What is the correct binary representation of 95?
What is wrong with the student's calculation method? What is the correct uncompressed size (24-bit RGB)?
Identify at least two errors in this reasoning.
How many milliseconds are in one minute? What is the maximum 8-bit value? Why will this application fail?
These questions connect Chapter 4 to ideas from Chapters 1–3.
1010 1100 to decimal. Show your work.0011 + 0101 in binary. Give the result in binary and decimal.| Term | Definition |
|---|---|
bit | Smallest unit of digital info: 0 or 1 |
byte | 8 bits; can represent 256 values |
binary | Base-2 number system using only 0 and 1 |
ASCII | 128-character English encoding, 7 bits |
Unicode | Universal standard for all writing systems |
UTF-8 | Variable-length Unicode encoding; dominant on the web |
pixel | Smallest unit of a digital image |
RGB | Red, Green, Blue color model; 3 bytes per pixel |
sampling | Measuring an analog signal at regular intervals |
sampling rate | Samples per second; CD = 44,100 Hz |
lossless | Compression with perfect reconstruction |
lossy | Compression that permanently discards data |
overflow | Value exceeds maximum representable in bit width |
floating-point | Binary approximation of real numbers |
aliasing | Distortion from undersampling high frequencies |
Chapter 4 explained how computers represent information — all of it, ultimately, as patterns of 0s and 1s. Chapter 5 takes the next step: how does the computer decide what to do with that information? Algorithms are precise, step-by-step procedures for processing information and producing results. Chapter 5 introduces the concept of an algorithm, shows how algorithms can be written as pseudocode or drawn as flowcharts, and builds the skills of designing, tracing, and evaluating algorithms by hand. The programming strand of this textbook begins in earnest in Chapter 5 and continues through Chapter 10.