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 pixel
2 Same colour as the run we are counting? Add 1 to the run count
3 Different colour? Write the finished run out as: frequency, then data
4 Start a new run with this pixel (count = 1)
5 End of the row? Write out the last run
Pixel readRun so farPairs written
1: WW x 1(none yet)

Output

 

Step 1: Pixel 1 is W. No run yet, so we start one and count 1.

1 / 11

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.

Step 1 of 11: Pixel 1 is W. No run yet, so we start one and count 1..

Exam line: Each run becomes a pair: frequency first (how many), then data (what). Write the pair when the value changes, and don't forget the final run at the end of the row.
Watch out: A pair is only written when the run ENDS. While the same colour keeps coming, the encoder is just counting.

Your turn · Encode, then decode

Now you do the encoding

Compress the bit pattern 111100000011 with run-length encoding, then rebuild it and see whether anything went missing. Along the way, decode a short list of letters too.

  1. Split the pattern into runs of identical bits: 1111 | 000000 | 11. That's four 1s, then six 0s, then two 1s (4 + 6 + 2 = 12, so every bit is accounted for).
  2. missing step
Which line is step 2?

Exam line: Encode: count each run, then write frequency then data. Decode: write each data value out its frequency number of times, in order.

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?

Check your thinking

Which idea is closest to yours?

A friend compresses some data with run-length encoding and then asks you a question about it.

Which of these is closest to what you think right now?
How sure are you?

Exam line: Savings come from long runs, decoding rebuilds the data exactly, and RLE works on any sequence of values that has runs.

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

1Run-length encoding is a compression method: each run of identical values is replaced by a frequency/data pair.
2It is lossless. Decoding the pairs gives back exactly the original data, so nothing has been discarded.
3To judge whether it saved space, count the values stored before and after. Each pair stores two values.
4A run of one value doubles in size, a run of two stays the same, and a run of three or more shrinks.
5RLE only spots identical values side by side. A repeating pattern such as ABABAB has no runs, so it does not shrink.
6It works on any sequence of values, including text, bit patterns and rows of pixels in an image.

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?
A stretch of the same value repeated one straight after another, such as BBBB in BBBBWW.
What does a frequency/data pair record?
How many times the value repeats (frequency), then the value itself (data): 'how many, then what'.
How do you decode a list of frequency/data pairs?
Write each data value out as many times as its frequency says, keeping the pairs in order.
What does it mean to say RLE is lossless?
Decoding rebuilds the original data exactly. Nothing is thrown away to get the saving.
When does RLE save the most space?
When the data has long runs of the same value, because a whole run shrinks to a single pair.
When can RLE make data bigger?
When values rarely repeat. Each run of one value needs a frequency as well, so it doubles in size.
Why count values, not pairs, when judging an RLE saving?
Because every pair is two stored values: a frequency and a data value. Compare the totals either side.
What happens to a run of 1, a run of 2 and a run of 3 or more?
A run of 1 doubles (1 value becomes 2), a run of 2 stays the same size, a run of 3 or more gets smaller.
Does ABABAB shrink under RLE?
No. RLE only spots identical values side by side, and a repeating pattern has no runs: it stores 1 A 1 B 1 A 1 B 1 A 1 B.
What kinds of data can RLE be used on?
Any sequence of values that can contain runs: text, binary data and the pixels of an image.

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 posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

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.