Example 1 — Run-length encode a pixel row
Show the steps and the answer
Split the row into runs: 12 W, 6 B, 6 W.
Run-length encoding replaces each stretch of one colour with that colour and the length of the stretch.
Encode each run in 5 bits (1 colour bit + 4 count bits): 3 runs × 5 bits = 15 bits.
Both counts fit in 4 bits (12 is 1100, 6 is 0110), so every run costs the same 5 bits.
Compute the ratio: 24 ÷ 15 = 1.6.
Compression ratio is original size over compressed size, so the row now needs 15/24 = 62.5% of its old space.
Decode to check: 12 W, 6 B, 6 W rebuilds all 24 pixels in order.
Nothing was rounded or dropped, so this method is lossless.
Answer: 15 bits instead of 24: a compression ratio of 1.6, and the row needs 62.5% of its original space.
Takeaway: Run-length encoding saves space when data has long runs of the same value, and decoding gives back every bit.