GCSE · Computer Science · AQA · Spec 8525
Run-length encoding (RLE)
Nobody reads a pixel row as 'white, white, white, white, white...'. You'd say 'five white, three black, two white'. That's run-length encoding, and it can backfire.
Computer Science · Data compression
Say it out loud: five white, three black, two white
Step along one row of black-and-white pixels (W = white, B = black). The encoder counts each run, then writes it down as a pair.
Before you reach pixel 6, where the colour changes from W to B: what will the encoder write down at that moment?
1 Read the next pixel2 Same colour as the run we are counting? Add 1 to the run count3 Different colour? Write the finished run out as: frequency, then data4 Start a new run with this pixel (count = 1)5 End of the row? Write out the last run
| Pixel read | Run so far | Pairs written |
|---|---|---|
| 1: W | W x 1 | (none yet) |
Output
Step 1: Pixel 1 is W. No run yet, so we start one and count 1.
Press Next to read one pixel at a time. Watch 'Run so far' grow, then watch what pops up in 'Pairs written' the moment the colour changes.
Predict, then check
Two short pieces of data. RLE will help one of them. What does it do to the other?
Data 1 is AAAAAABBBB. Data 2 is ABCD. Both get turned into frequency/data pairs. Which prediction is right?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Say how many, then what, and a long boring row of data shrinks to a few pairs.
What you need to know
- RLE hunts for runs: the same value repeated back to back, with nothing different in between.
- Each run is swapped for one pair: the frequency (how many) comes first, then the data (what).
- So AAAABBBCCD becomes 4 A, 3 B, 2 C, 1 D. Ten letters, four pairs.
Have a goYour turn: write the pixel row WWWBBBBW as frequency/data pairs.
3 W 4 B 1 W
Three Ws, four Bs, then one lonely W. That last W is a separate run, so it gets its own pair instead of joining the first Ws.
- A pair only gets written when its run ends, and the very last run needs writing out too.
- Decoding runs it backwards: write each data value out its frequency number of times, keeping the pairs in order.
- You get the original back exactly, with nothing thrown away. That's what lossless means.
Have a goYour friend decodes 3 Z 1 Q, writes 'ZQ', and announces that the numbers are 'just there for decoration'. What should they have written?
ZZZQ
The numbers are the whole point: each frequency says how many copies to write. Ignore them and the data is lost, which RLE never does.
- RLE isn't just for text. Bit patterns and rows of pixels are sequences of values too, so runs show up there.
- Every pair costs two stored values, so judge a saving by counting values before and after, never pairs.
Have a goA row of 9 pixels is WWWWWWWWB. How many values does RLE store for it, and is that a saving?
4 values (8 W 1 B), down from 9, so yes
Two runs make two pairs, and each pair is two values. Counting pairs would give 2 and make the saving look bigger than it is.
- Long runs shrink brilliantly. Data that rarely repeats can grow: ABCD becomes 1 A 1 B 1 C 1 D.
The big picture
Run-length encoding squashes data by swapping each run of identical values for a pair: how many times it repeats (the frequency), then the value itself (the data). Decoding rebuilds the original exactly, so nothing is lost. Long runs mean a big saving; data where nothing repeats can actually get bigger.
Key points
Worked example
Problem
Compress the 15 characters KKKKKKQQQQQQQRS using run-length encoding. Then decide whether RLE has saved space by counting the values stored before and after.
⚠ Watch out
Believing compression always makes data smaller. It feels obvious, because squashing is the whole job. But every run, even a run of one, needs its own frequency, so data with few repeats can grow. And count values, not pairs: one pair is two values.
Memory hook
'How many, then what.' Say it every single time you write a pair. And never forget poor ABCD: when nothing repeats, RLE turns 4 values into 8.
Check yourself
A row of 20 pixels is all black. What does RLE store, and what kind of row would stop it helping? (Answer: 20 B, two values for twenty; rows of mostly one- or two-pixel runs.)
Flashcards
(10)What is a 'run' in run-length encoding?
What does a frequency/data pair record?
How do you decode a list of frequency/data pairs?
What does it mean to say RLE is lossless?
When does RLE save the most space?
When can RLE make data bigger?
Why count values, not pairs, when judging an RLE saving?
What happens to a run of 1, a run of 2 and a run of 3 or more?
Does ABABAB shrink under RLE?
What kinds of data can RLE be used on?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore AQA GCSE Computer Science topics
How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 30 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.