Year 10 · Computer Science · Autumn 1

Data representation · Lesson 5 of 5

Compression and the exam questions

Exam practice1.3 Data storage and compression

Year 10 · Computer Science · Data representation · Lesson 5 of 5

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.

Year 10 · Computer Science · Data representation · Lesson 5 of 5

Success criteria

You have got it when you can say:

  • 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.

Year 10 · Computer Science · Data representation · Lesson 5 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    In 8-bit two's complement, 11111111 is…

    A −127B 255C −1

    C. −1

  2. Earlier this unit

    Which of these is lossy?

    A PNGB JPEGC ZIP

    B. JPEG

Year 10 · Computer Science · Data representation · Lesson 5 of 5

Key words (1 of 3)

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.

Year 10 · Computer Science · Data representation · Lesson 5 of 5

Key words (2 of 3)

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.

Year 10 · Computer Science · Data representation · Lesson 5 of 5

Key words (3 of 3)

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 · step 1 of 9 · part 1 of 3

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?

The explanation · step 1 of 9 · part 2 of 3

Units of data (continued)

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⁶⁰

The explanation · step 1 of 9 · part 3 of 3

Units of data (continued)

!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.

The explanation · step 2 of 9 · part 1 of 2

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.

The explanation · step 2 of 9 · part 2 of 2

Converting between units (continued)

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.

The explanation · step 2 of 9

The maths: Changing units

The explanation · step 3 of 9 · part 1 of 3

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.

The explanation · step 3 of 9 · part 2 of 3

Calculating image file size (continued)

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.

The explanation · step 3 of 9 · part 3 of 3

Calculating image file size (continued)

!Watch out

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

The explanation · step 4 of 9 · part 1 of 3

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.

The explanation · step 4 of 9 · part 2 of 3

Calculating sound file size (continued)

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.

The explanation · step 4 of 9 · part 3 of 3

Calculating sound file size (continued)

!Watch out

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

The explanation · 5 more steps

More on the lesson page

The lesson page has the rest of the explanation, step by step:

  • Why we compress files
  • Lossy compression
  • Lossless compression
  • Run-length encoding (RLE)
  • RLE: working out the saving

Open the lesson page

Year 10 · Computer Science · Data representation · Lesson 5 of 5

Try it: Run-length encoder

Year 10 · Computer Science · Data representation · Lesson 5 of 5

The maths: How much did compression save?

Symbols first, then the words, then the equation. Change the numbers.

Year 10 · Computer Science · Data representation · Lesson 5 of 5

Quick quiz · 1 of 3

A 10 × 10 image at 8 bits per pixel takes…

  1. A 8,000 bits
  2. B 800 bits = 100 bytes
  3. C 80 bytes

B. 800 bits = 100 bytes

10 × 10 × 8 = 800 bits. Divide by 8 for bytes.

Year 10 · Computer Science · Data representation · Lesson 5 of 5

Quick quiz · 2 of 3

How many bits are in a byte?

  1. A 16
  2. B 4
  3. C 8

C. 8

A byte is 8 bits. 4 bits is a nibble.

Year 10 · Computer Science · Data representation · Lesson 5 of 5

Quick quiz · 3 of 3

Hex B2 in binary is…

  1. A 1100 0010
  2. B 1011 0010
  3. C 1011 0100

B. 1011 0010

B = 11 = 1011 and 2 = 0010. One hex digit is 4 bits.

Year 10 · Computer Science · Data representation · Lesson 5 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. Adding 11001100 and 01011010 in an 8-bit register gives…

    A 11111111B An overflow errorC 00100110 with no problem

    B. An overflow error The true answer needs 9 bits (100100110). The ninth bit does not fit.

  2. Shifting 00001010 (10) one place left gives…

    A 00010100 (20)B 00001011 (11)C 00000101 (5)

    A. 00010100 (20) A left shift doubles. 10 becomes 20.

Year 10 · Computer Science · Data representation · Lesson 5 of 5

Homework and the challenge

Homework

The CS 1 quiz.

End-of-unit challenge

End-of-unit test: past-paper questions on topic 1 (about 25 marks, 25 minutes).