Data compression reduces the number of bits needed to store or send data. Lossless compression lets you rebuild the original exactly; lossy compression throws away some detail to make the data smaller, so the original can only be approximated. You choose between them by asking which matters more for the purpose: exact data or small size.
Quick check
How many bits are in a byte?Show answerHide answer
- A byte is a group of 8 bits.
How could you write AAAABB more briefly, using counts?Show answerHide answer
4A2B: four As, then two Bs. Each run of a repeated letter becomes a count and the letter.
Know these already? Jump to the practice questions
What you'll be able to do
- Explain how compression reduces the number of bits.
- Apply run-length encoding to compress and decompress text.
- Compare what lossless and lossy compression keep.
- Choose a type of compression for a stated purpose.
Key terms
Data compression
Reducing the number of bits used to store or send data.
- In plain English
- Making data take up less space.
- Don't write
- "Compression makes every file smaller."
Lossless compression
Compression from which the original data can be rebuilt exactly, bit for bit.
- In plain English
- Smaller, with nothing lost.
- Don't write
- "Lossless compression throws away only unimportant data."
Lossy compression
Compression that removes some information, so decompressing gives only an approximation of the original.
- In plain English
- Much smaller, but some detail is gone for good.
- Don't write
- "The right program can restore a lossy file to the original."
Run-length encoding
A lossless method that replaces each run of a repeated value with a count and one copy of the value.
- In plain English
- Writing "5A" instead of "AAAAA".
- Don't write
- "Run-length encoding removes the repeated letters."
What does compression save?
Compression works by removing redundancy: repetition that the data does not need to spell out every time. Fewer bits means less storage space and faster transfer over a network. The cost is processing time, because the data must be compressed before it is stored or sent and decompressed before it is used.
How much you save depends on the data. A picture with large areas of one color has long runs of identical pixels, so it compresses well. Data with little repetition compresses only a little, and some data cannot be shrunk at all.
Everything about compression is Learn this: the exam reference sheet covers only the pseudocode.
Check yourself
Show answerHide answer
The 20 white pixels. They form one run, stored as one count and one value; the alternating row has 20 runs of length 1.
How does lossless compression rebuild the original?
Run-length encoding (RLE) replaces each run of a repeated value with a count and the value. AAAAABBB (8 characters) becomes 5A3B (4 characters). To decompress, you expand each pair: 5A gives AAAAA and 3B gives BBB, which is exactly the original. Because every character comes back, RLE is lossless.
Another lossless idea replaces a repeated word or pattern with a short code and stores a table of the codes. The table counts toward the compressed size, but the original can still be rebuilt exactly.
Lossless compression is used where every bit matters. ZIP files and PNG images, for example, use lossless compression.
Key idea
Lossless means the decompressed data is identical to the original, bit for bit.
Check yourself
Show answerHide answer
XXXYXX: three Xs, one Y, then two Xs.
What does lossy compression throw away?
Lossy compression removes detail that people are unlikely to notice, such as tiny color differences in a photo or sounds the ear is unlikely to pick out. Because it is allowed to discard information, it can usually make data much smaller than lossless compression can.
The discarded detail is never stored, so it is gone for good. Decompressing a lossy file gives an approximation that looks or sounds close to the original, not a copy of it.
The common JPEG image format and MP3 audio both use lossy compression.
Check yourself
Show answerHide answer
No. Some detail was discarded during compression, so the reopened photo is only an approximation.
Which method fits the purpose?
Read the question for what the user needs, then match it.
| The purpose says… | Choose | Because |
|---|---|---|
| The original must be rebuilt exactly (text, program code, records) | Lossless | One changed bit can change the meaning |
| The file must be as small or as fast to send as possible, and some loss of quality is acceptable (streaming video, web photos) | Lossy | It shrinks data much further |
Words such as "exactly", "every detail" or "the original" point to lossless. Words such as "as small as possible", "quickly" or "slight loss is acceptable" point to lossy.
Worked example 1: Encode a row of pixels
Question: A row of pixels is stored as the text WWWWWBBBWWWWWWWW, where W is white and B is black. (a) Write it with run-length encoding, as a count then a letter. (b) How many characters does this save?
What tells you the method
Repeated letters in a row: find each run, then write its count and letter.
Step 1: Split the row into runs.
WWWWW, BBB, WWWWWWWW: runs of 5, 3 and 8.
WhyA run ends as soon as the letter changes.
Step 2: Write each run as count and letter.
5W3B8W.
WhyEach run becomes one count and one copy of the letter.
Step 3: Compare the lengths.
The original has 16 characters and the encoding has 6, so 16 − 6 = 10 characters are saved.
WhyThe saving is the difference in length.
Check it
Decompress 5W3B8W: 5 + 3 + 8 = 16 characters, in the same order as the original.
Answer
(a) 5W3B8W; (b) 10
Worked example 2: Choose compression for two purposes
Question: A school stores two kinds of files. Essays must be restored word for word. Video clips for its website should load quickly, and a slight loss of sharpness is acceptable. Which choice fits both purposes?
- A) Lossy for both
- B) Lossless for the essays and lossy for the videos
- C) Lossy for the essays and lossless for the videos
- D) Lossless for both, because lossless always gives the smallest files
What tells you the method
Two stated purposes: match each one to the table in the section on choosing a method.
Step 1: Decide for the essays.
"Restored word for word" means the original must be rebuilt exactly, so lossless.
WhyLossy compression could change the text.
Step 2: Decide for the videos.
"Load quickly" and "slight loss is acceptable" point to lossy.
WhyLossy makes the files much smaller, so they load faster.
Check it
A fails the essays, C has both choices swapped, and D's reason is false: lossy usually gives smaller files.
Answer
B
Worked example 3: Trace a run-counting procedure
Question: Each run needs one count–value pair in RLE, so this code counts the runs in a row of pixels (0 is white, 1 is black). What is displayed as a result of running it?
pixels ← [0, 0, 0, 1, 1, 0, 0]
runs ← 1
i ← 2
REPEAT UNTIL(i > LENGTH(pixels))
{
IF(pixels[i] ≠ pixels[i - 1])
{
runs ← runs + 1
}
i ← i + 1
}
DISPLAY(runs)
What tells you the method
A loop over list indexes: trace each pass in a table.
Step 1: Trace the loop.
ipixels[i]pixels[i - 1]Different? runs2 0 0 no 1 3 0 0 no 1 4 1 0 yes 2 5 1 1 no 2 6 0 1 yes 3 7 0 0 no 3 Whyrunsstarts at 1 for the first run, and a new run starts each time a pixel differs from the one before it.Step 2: Stop the loop.
After the pass with
i= 7,ibecomes 8, which is greater thanLENGTH(pixels)= 7, so the loop ends.WhyIndexes start at 1, so the last element is at index 7.
Check it
The row splits into 000, 11 and 00: 3 runs, encoded as 3 pairs.
Answer
3
Your turn
Question: The text 4R2G3R was made with run-length encoding, as a count then a letter. How many characters long is the original text?
Step 1: Expand the first pair.
4R gives RRRR.
Step 2: Your go: expand the other pairs and count the characters.
Show the full solutionHide the solution
Step 2. 2G gives GG and 3R gives RRR, so the original is RRRRGGRRR. That is 4 + 2 + 3 = 9 characters.
Answer: 9
Traps
Trap
Compression always makes data smaller.
It seems right because making data smaller is the whole point. But a method only saves space when the data has the redundancy it looks for: RLE turns ABCD (4 characters) into 1A1B1C1D (8 characters).
How many characters is the run-length encoding of XYZ?
- Each letter is a run of 1, so the encoding is 1X1Y1Z.
Trap
The right software can restore a lossy file to the original.
It seems right because a decompressed photo looks complete. But lossy compression never stored the discarded detail, so no program can bring it back; only lossless compression rebuilds the original.
Which type of compression guarantees the original can be rebuilt?
Lossless compression.
What's examined
On the AP® Computer Science Principles exam, compression belongs to Big Idea 2: Data. Expect multiple-choice questions that ask you to identify lossless or lossy compression, choose one for a described purpose, or apply a simple compression scheme to a short string and compare lengths. For representing data in bits, see the note on binary numbers and bits.
Practice questions
- Recall Which statement describes lossless compression?
- A) Some data is removed, so the result approximates the original.
- B) It reduces the bits by sampling the data at intervals.
- C) The original data can be rebuilt exactly from the compressed data.
- D) It works only on images and sound.
Show answer
Answer: C. Lossless compression keeps all the information, so decompressing gives the original bit for bit.
Why the others are wrong:
- A describes lossy compression.
- B describes sampling analog data.
- D is false: lossless compression works on any data, including text.
- Recall A writer compresses a novel's manuscript and must get back every word exactly. Which type of compression fits? Answer lossless or lossy.
Show answer
Answer: Lossless
Marking points:
- The manuscript must be rebuilt exactly, and only lossless compression guarantees that.
- Apply Which is the run-length encoding of KKKKKKPPPKK, written as a count then a letter?
- A) 6K3P2K
- B) 6K3P
- C) 5K3P2K
- D) 8K3P
Show answer
Answer: A. The runs are six Ks, three Ps and two Ks.
Why the others are wrong:
- B drops the last run, so decompressing loses two Ks.
- C miscounts the first run by one.
- D merges the two separate K runs into one.
- Apply Using run-length encoding written as a count then a letter, how many characters long is the encoding of a string of 12 As followed by 3 Bs?
Show answer
Answer: 5
Marking points:
- The encoding is 12A3B.
- The count 12 takes two characters, so the length is 2 + 1 + 1 + 1 = 5.
- Exam-style A photo app must keep image files as small as possible, and users accept a slight loss of detail. A tax office must be able to rebuild every scanned form exactly. Which choice fits both?
- A) Lossless for the app and lossy for the tax office
- B) Lossy for both
- C) Lossless for both
- D) Lossy for the app and lossless for the tax office
Show answer
Answer: D. The app's goal is the smallest file with acceptable loss, so lossy; the tax office needs exact rebuilding, so lossless.
Why the others are wrong:
- A swaps the two choices.
- B fails the tax office, which needs the exact original.
- C fails the app's goal, because lossy compression usually makes files much smaller.
- Exam-style This code should count the runs in
pixels. Which condition can replace<MISSING CONDITION>so that it works for every non-empty list?
runs ← 1
i ← 2
REPEAT UNTIL(i > LENGTH(pixels))
{
IF(<MISSING CONDITION>)
{
runs ← runs + 1
}
i ← i + 1
}
- A)
pixels[i] = pixels[i - 1] - B)
pixels[i] ≠ pixels[i - 1] - C)
pixels[i] ≠ pixels[i + 1] - D)
pixels[i] ≠ pixels[1]
Show answer
Answer: B. A new run starts exactly when a pixel differs from the one before it. For [0, 0, 1, 1, 1, 0] it gives 3, the correct count.
Why the others are wrong:
- A counts repeats instead of changes: it gives 4 for [0, 0, 1, 1, 1, 0].
- C reads
pixels[i + 1]wheniis the last index, which is outside the list, so the program stops with an error. - D compares with the first pixel only: it gives 4 for [0, 0, 1, 1, 1, 0].
- Exam-style What is displayed as a result of running this code segment?
row ← [5, 5, 5, 5, 2, 2, 5]
prev ← row[1]
changes ← 0
FOR EACH v IN row
{
IF(v ≠ prev)
{
changes ← changes + 1
prev ← v
}
}
DISPLAY(changes + 1)
Show answer
Answer: 3
Marking points:
prevstarts as 5, so the first four values make no change.- At the first 2,
changesbecomes 1 andprevbecomes 2; the second 2 makes no change. - At the last 5,
changesbecomes 2, so the display is 2 + 1 = 3, one per run.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- Compression cuts the bits needed by removing redundancy, which saves storage and transfer time.
- Run-length encoding stores each run as a count and a value, and decompresses to the exact original.
- Lossy compression discards detail for much smaller files; the original cannot be recovered.
- Exact rebuilding needs lossless; smallest size with acceptable quality loss suits lossy.
- Data with little repetition may not shrink at all, and can even grow.