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

Text, images and sound as bits

What this lesson covers

1.2 Text, sound and images

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

1.3 Data storage and compression

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

You will learn to

  • Explain how a character set, such as ASCII or Unicode, turns text into binary.
  • Compare ASCII and Unicode, including why Unicode files are usually larger.
  • Describe how resolution, colour depth, sample rate and sample resolution affect quality and file size.
  • Calculate the file size of an image and of a sound file, with units on every line.

Success criteria

  • I can say why Unicode can show more languages than ASCII.
  • I can explain what happens to quality and file size when the sample rate goes up.
  • I can work out that a 1024 × 768 image with 24-bit colour is 2.25 MiB.
  • I can work out the size of a sound file from its sample rate, resolution and length.
The syllabus points for these sections
  • Explain how a character set turns text into binary.
  • Compare ASCII and Unicode.
  • Explain how sound is sampled, using sample rate and sample resolution.
  • Explain how images are stored as pixels, using resolution and colour depth.
  • Describe how each of these settings affects quality and file size.
  • 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.
Denary
The base 10 number system we use every day, with digits 0 to 9.
Binary
The base 2 number system computers use, with digits 0 and 1.
Character set
A list of characters a computer can use, each with its own binary code.
ASCII
A character set using 7 bits per character, giving 128 characters, mainly for English.
Unicode
A character set using more bits per character, covering most world languages, symbols and emojis.
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.

1.2 Text, sound and images

Character sets

A computer can only store numbers. So every character is given a number code.

A character is any letter, digit, punctuation mark, space or symbol.

A character set is the full list of characters a computer can use, with the binary code for each one.

When you press a key, the keyboard sends the code. The screen looks up the code and draws the character.

e.g.Example
The word Hello in ASCII: H = 72 = 01001000 e = 101 = 01100101 l = 108 = 01101100 l = 108 = 01101100 o = 111 = 01101111 5 characters × 8 bits = 40 bits
?Why?

Both computers must use the same character set. If they do not, a message arrives as strange symbols.

ASCII

ASCII (American Standard Code for Information Interchange) uses 7 bits per character. That gives 2⁷ = 128 characters.

It covers English letters, digits, punctuation and some control codes like "new line".

Extended ASCII uses 8 bits, giving 256 characters. The extra ones add some accented letters and symbols.

Codes run in order. A is 65, so B is 66 and C is 67. Lowercase a is 97, which is 32 more than A.

CharacterDenary codeBinary (8 bits)
A6501000001
C6701000011
a9701100001
0 (the digit)4800110000
space3200100000
!Watch out

The digit 0 is not stored as 00000000. As a character it has the code 48. A digit in text is a character, not a number.

Unicode

Unicode is a much bigger character set. It can use up to 32 bits (4 bytes) per character.

It has room for over a million characters. It covers almost every written language, maths symbols and emojis.

The first 128 Unicode codes are the same as ASCII, so ASCII text still works.

The cost: Unicode uses more bits per character, so text files can be bigger.

ASCIIUnicode
Bits per character7 (8 for extended)More: up to 32
Number of characters128 (256 extended)Over a million
LanguagesEnglish onlyAlmost all world languages
Symbols and emojisVery few, no emojisYes, including emojis
File size for the same textSmallerCan be larger
e.g.Example
Spanish ñ has the Unicode code 241. It is not in 7-bit ASCII at all. The grinning face emoji has the code 1F600 in hex (128,512 in denary). It needs at least 17 bits. A Madrid school website in Spanish, Arabic and Chinese needs Unicode.
?Why?

ASCII was designed for English in the 1960s. The internet joined the whole world, so a far larger set was needed.

Sound: sampling

Real sound is a wave. It changes smoothly, so it is called analogue.

A computer cannot store a smooth wave. It measures the height (amplitude) of the wave at fixed time gaps.

Each measurement is a sample. Each sample is stored as a binary number.

This is called sampling. The stored samples are a close copy of the wave, not a perfect one.

There is a diagram for this step in the IGCSE library.

Sample rate and sample resolution

Sample rate is the number of samples taken each second. It is measured in hertz (Hz). 44,100 Hz means 44,100 samples every second.

Sample resolution is the number of bits used to store each sample. 16 bits gives 2¹⁶ = 65,536 possible heights.

A higher sample rate catches quicker changes in the wave. A higher sample resolution measures each height more precisely.

Both make the recording more accurate and closer to the real sound. Both also make the file bigger.

RecordingSample rateSample resolutionQuality
Old phone call8,000 Hz8 bits (256 levels)Clear speech, poor music
Music CD44,100 Hz16 bits (65,536 levels)High quality
Studio master96,000 Hz24 bits (16,777,216 levels)Very high, very large files
!Watch out

Do not mix the two up. Rate is how often you measure. Resolution is how precisely you measure each time.

Images: pixels and resolution

A bitmap image is a grid of tiny squares called pixels (picture elements).

Each pixel is one colour. Each colour is stored as a binary number.

Image resolution is the number of pixels in the image. It is written as width × height, for example 1920 × 1080.

More pixels show more detail, so the image is sharper. But more pixels need more bits, so the file is bigger.

e.g.Example
A 1920 × 1080 image has 1920 × 1080 = 2,073,600 pixels. A 640 × 480 image has 307,200 pixels. Enlarged to the same size, it looks blocky (pixelated).
?Why?

Each pixel needs its own colour code. Double the pixels and you double the data.

Colour depth

Colour depth is the number of bits used to store the colour of each pixel.

With n bits per pixel you can show 2ⁿ different colours.

Greater colour depth gives more colours, so shades look smoother and more real. It also makes the file bigger.

24-bit colour uses 8 bits for red, 8 for green and 8 for blue. That is two hex digits each, as in #FF8800.

Colour depthNumber of coloursTypical use
1 bit2¹ = 2Black and white only
8 bits2⁸ = 256Simple logos and icons, GIF images
16 bits2¹⁶ = 65,536Older phone screens
24 bits2²⁴ = 16,777,216Photos (true colour)
e.g.Example
Colour depth 8 bits: 2⁸ = 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 = 256 colours. Going from 8 bits to 16 bits doubles the file size but gives 256 times as many colours.
!Watch out

Colour depth is bits per pixel, not the number of colours. 8-bit colour means 256 colours, not 8.

1.3 Data storage and compression

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.