Skip to the page
Year 10 · Computer Science · Autumn 1 · Data representation

Compression and the exam questions

Exam practicePresent ▶

What this lesson covers

1.3 Data storage and compression

Cambridge IGCSE Computer Science 0478 · Topic 1: Data representation · Paper 1

You will learn to

  • Explain why files are compressed: to save storage space and to send them faster.
  • Compare lossy and lossless compression, with an example file type for each.
  • Use run-length encoding (RLE) on a string of data and work out the space saved.
  • Answer past-paper questions on topic 1, using the command word to shape each answer.

Success criteria

  • I can encode a short string with RLE and say how many bytes it saves.
  • I can choose lossy or lossless for a photo, a text file and a program, and say why.
  • I can mark my own answer with the mark scheme and find the missing point.
The syllabus points for this section
  • Name the units of data from bit to EiB and convert between them using 1024.
  • Calculate the file size of an image and of a sound file, showing working.
  • Explain why files are compressed.
  • Compare lossy and lossless compression and give examples of each.
  • Use run-length encoding (RLE) and work out the space saved.

Key words

Say each word, then read what it means. Every one is in the glossary too.

Bit
A single binary digit, 0 or 1.
Nibble
A group of 4 bits, which matches one hexadecimal digit.
Byte
A group of 8 bits.
Character set
A list of characters a computer can use, each with its own binary code.
Sample rate
The number of sound samples taken each second, measured in hertz (Hz).
Sample resolution
The number of bits used to store each sound sample.
Pixel
The smallest single-coloured square in a bitmap image.
Image resolution
The number of pixels in an image, often written as width × height.
Colour depth
The number of bits used to store the colour of each pixel.
Lossy compression
Making a file smaller by permanently removing some data.
Lossless compression
Making a file smaller without losing any data, so the original can be rebuilt exactly.
Run-length encoding (RLE)
A lossless method that stores each run of repeated values as a count and the value.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

Units of data

A bit is one 0 or 1. A nibble is 4 bits. A byte is 8 bits.

After the byte, each unit is 1024 times bigger than the one before.

1024 is used because it is a power of 2: 2¹⁰ = 1024. Computers count in powers of 2.

Try the toy: type 1 and choose GiB. Read the MiB and KiB rows in the table. How many times did you multiply by 1024 to reach KiB?

UnitEqual toIn bytes
1 nibble4 bitshalf a byte
1 byte (B)8 bits1
1 kibibyte (KiB)1024 bytes1,024
1 mebibyte (MiB)1024 KiB1,048,576
1 gibibyte (GiB)1024 MiB1,073,741,824
1 tebibyte (TiB)1024 GiBabout 1.1 million million
1 pebibyte (PiB)1024 TiB2⁵⁰
1 exbibyte (EiB)1024 PiB2⁶⁰
!Watch out

Cambridge uses 1024, not 1000. Shops often say KB, MB and GB meaning 1000. In the exam, always use 1024 and the KiB, MiB, GiB names.

There is something to try for this step in the IGCSE library.

Converting between units

Going to a smaller unit: multiply (× 1024, or × 8 from bytes to bits).

Going to a bigger unit: divide (÷ 1024, or ÷ 8 from bits to bytes).

Move one unit at a time and write each step. It is slower but it avoids mistakes.

e.g.Example
3 MiB in bytes: 3 × 1024 = 3072 KiB 3072 × 1024 = 3,145,728 bytes 65,536 bits in KiB: 65,536 ÷ 8 = 8192 bytes 8192 ÷ 1024 = 8 KiB 2 GiB in MiB: 2 × 1024 = 2048 MiB
?Why?

Paper 1 has no calculator. Exam numbers are usually chosen so that dividing by 8 and 1024 comes out neatly.

Calculating image file size

Every pixel needs the same number of bits: the colour depth.

So file size in bits = number of pixels × colour depth. Number of pixels = width × height.

Then divide by 8 for bytes, by 1024 for KiB, and by 1024 again for MiB.

Real files are a little bigger, because they also store extra data such as the width and height.

e.g.Example
An image is 1024 × 512 pixels with a colour depth of 16 bits. Find its size in MiB. Step 1, pixels: 1024 × 512 = 524,288 pixels Step 2, bits: 524,288 × 16 = 8,388,608 bits Step 3, bytes: 8,388,608 ÷ 8 = 1,048,576 bytes Step 4, KiB: 1,048,576 ÷ 1024 = 1024 KiB Step 5, MiB: 1024 ÷ 1024 = 1 MiB Tip: 16 bits = 2 bytes. So 524,288 × 2 = 1,048,576 bytes in one go.
!Watch out

Do not forget to divide by 8. Pixels × colour depth gives bits, not bytes.

Calculating sound file size

Each second you take (sample rate) samples. Each sample uses (sample resolution) bits.

So file size in bits = sample rate × sample resolution × length in seconds.

Then divide by 8 for bytes and by 1024 for KiB or MiB, as for images.

This is for one channel (mono). Change minutes into seconds first.

e.g.Example
A sound is recorded at 8000 Hz with a sample resolution of 16 bits. It lasts 32 seconds. Find its size in KiB. Step 1, bits per second: 8000 × 16 = 128,000 bits Step 2, total bits: 128,000 × 32 = 4,096,000 bits Step 3, bytes: 4,096,000 ÷ 8 = 512,000 bytes Step 4, KiB: 512,000 ÷ 1024 = 500 KiB CD quality for 1 minute: 44,100 × 16 × 60 = 42,336,000 bits = 5,292,000 bytes, about 5 MiB.
!Watch out

A length of 2 minutes is 120 seconds. Using 2 in the formula is a very common lost mark.

Why we compress files

Compression makes a file smaller, so it uses fewer bits.

Smaller files use less storage space on a device or server.

Smaller files need less bandwidth. Bandwidth is how much data a connection can carry each second.

So smaller files are sent faster: quicker downloads, quicker uploads, and less buffering when streaming.

Many services set a size limit. An email attachment limit of 25 MB is a real example.

e.g.Example
A 5 MiB song is compressed to about 0.5 MiB as an MP3. A phone can now store about ten times as many songs, and each one downloads ten times faster.
?Why?

Storage and network time both cost money and battery. Sending fewer bits saves both.

Lossy compression

Lossy compression removes data permanently. You can never get the original back.

It removes detail people are unlikely to notice, such as sounds humans cannot hear or tiny colour differences.

Ways to do it: lower the image resolution or colour depth, or lower the sample rate or sample resolution.

Examples: JPEG for photos, MP3 for music, and most video streaming.

It gives a much smaller file than lossless, but quality is lower.

e.g.Example
A school uploads 200 sports-day photos to its website. Each is 6 MiB from the camera. Saving them as JPEGs at a lower resolution makes each about 300 KiB. The page loads quickly on phones, and visitors cannot see the difference on screen.
!Watch out

Never use lossy compression on text, program code or spreadsheets. Losing even one character can change the meaning or stop a program working.

Lossless compression

Lossless compression makes the file smaller without losing any data.

When the file is decompressed, it is exactly the same as the original, bit for bit.

It finds patterns and repeats in the data and stores them in a shorter way.

Use it when every bit matters: text, program files, spreadsheets, medical scans.

The file is not made as small as with lossy compression.

LossyLossless
Data removed?Yes, permanentlyNo
Original restored?NoYes, exactly
File sizeMuch smallerSmaller, but less saving
ExamplesJPEG, MP3RLE, ZIP, PNG
Good forPhotos, music, videoText, code, spreadsheets

Run-length encoding (RLE)

Run-length encoding (RLE) is a lossless method. A run is a group of the same value in a row.

RLE stores each run as two values: how many, then which value.

It works well when data has long runs, such as simple images with big areas of one colour.

Try the toy: press All one colour, then press Stripes so every pixel is different from the next. Compare the sizes before and after RLE.

e.g.Example
Text: AAAAABBBCCCCCCCC Runs: 5 A, 3 B, 8 C RLE: 5A 3B 8C Original: 16 characters. Encoded: 6 characters. A row of pixels, W = white, B = black: WWWWWWWWBBBWWWWWWBBBBB RLE: 8 W, 3 B, 6 W, 5 B
?Why?

Writing "8 white" once takes less space than writing "white" eight times. Decoding gives the exact row back, so no data is lost.

There is something to try for this step in the IGCSE library.

RLE: working out the saving

To judge compression, compare the size before and after.

RLE only helps when runs are long. If values change all the time, RLE can make the file bigger.

That is why RLE suits simple graphics and icons, but not photographs.

e.g.Example
The pixel row WWWWWWWWBBBWWWWWWBBBBB, with 1 byte per pixel. Before: 22 pixels × 1 byte = 22 bytes After: 4 runs × 2 bytes (count + colour) = 8 bytes Saving: 22 − 8 = 14 bytes Percentage saved: 14 ÷ 22 × 100 = about 64% Now a row with no repeats: ABCD Before: 4 bytes RLE: 1A 1B 1C 1D = 8 bytes The file has doubled in size.
!Watch out

Do not say RLE always makes files smaller. With few repeats it can make them larger.

Study it

Open the study page and go one step at a time.

The steps for this lesson

Try it

The maths

Symbols first, then the words, then the equation. Change the numbers to try your own.

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.