Topic 1 · Data representation

Lossy and lossless compression

Video lesson: Lossy and lossless compression▶ Watch the video lessonShort, exam-focused explanation of this topic on the CS with Aamir YouTube channel.

Compression reduces the size of a file. You need to know why we compress files and the difference between the two types of compression: lossy and lossless.

Why compress files?

Lossy compression

Lossy compression permanently removes some data from the file. The original file cannot be recreated exactly. It is used where a small drop in quality is not noticed by people.

Lossy compression gives a much smaller file, but it is not suitable for text documents or program files, because losing any data would change their meaning or stop them working.

Lossless compression

Lossless compression makes the file smaller without losing any data. The original file can be recreated exactly when it is decompressed. It is used for text, program files and anything that must not change (e.g. ZIP files, PNG images).

Run-length encoding (RLE)

RLE is a lossless method. It replaces a run of repeated values with a single copy of the value and a count of how many times it repeats.

Example: AAAABBBCCD → 4A 3B 2C 1D
The original uses 10 characters; the compressed version stores just 4 pairs.

RLE works well on data with long runs of the same value, such as simple images with large areas of one colour. On data with few repeats it can even make the file bigger, because every single value needs a count as well.

Lossy or lossless? A quick comparison

LossyLossless
Data removed?Yes, permanentlyNo
Original can be restored?NoYes, exactly
File size reductionLargeSmaller
Typical usePhotos, music, video for the webText, programs, spreadsheets

Exam tips

Want the full topic notes, slides and worksheets? See Notes & Slides, practise with the free CS with Aamir Quiz App, or order the O Level Computer Science Study Guide.