Cambridge IGCSE Computer Science Notes

Chapter 1 · Data representation

1.3 Data storage and file compression

Digital data takes space. This topic explains how that space is measured, how to calculate the size of bitmap images and sound files, why files are compressed, and how lossy and lossless compression reduce file size in different ways.

Storage unitsFile-size calculationsLossy & losslessRun-length encoding

What you need to understand

By the end of Topic 1.3 you should be able to use binary data-storage units correctly, calculate the uncompressed size of bitmap images and sampled sound, explain why compression is needed, distinguish lossy from lossless compression, describe common lossy formats, and explain how run-length encoding compresses repeated data without losing the original information.

1.3.1

Measurement of data storage

All digital data is ultimately stored as binary. The smallest binary unit is a bit, which can have the value 0 or 1. The word bit comes from binary digit.

Bit

One binary digit: either 0 or 1.

Nibble

4 bits. A nibble is half a byte.

Byte

8 bits. Larger storage quantities are built from bytes.

Denary storage units and binary storage units

Two naming systems are commonly seen. The SI-style units use powers of 1000, while the IEC binary units use powers of 1024. The course book shows both so that you can recognise the difference.

Course-book Table 1.4 showing kilobyte, megabyte, gigabyte, terabyte, petabyte and exabyte using denary values
Course-book Table 1.4: storage units written using denary powers of 1000.
Course-book Table 1.5 showing kibibyte, mebibyte, gibibyte, tebibyte, pebibyte and exbibyte using powers of two
Course-book Table 1.5: the IEC binary storage system.
IEC unitSymbolBytesPower of 2
kibibyteKiB1,024210
mebibyteMiB1,048,576220
gibibyteGiB1,073,741,824230
tebibyteTiB1,099,511,627,776240
pebibytePiB1,125,899,906,842,624250
exbibyteEiB1,152,921,504,606,846,976260
Exam focus: the course book explicitly notes that the IEC system is the system covered by the syllabus. For calculations, remember that 1 KiB = 1024 bytes and each step to the next IEC unit multiplies by another 1024.

Worked conversion

Convert 8 MiB into bytes.

8 × 1024 × 1024 = 8,388,608 bytes

Going from MiB to bytes requires two ×1024 steps: MiB → KiB → bytes.

Useful pattern: KiB = 210 bytes, MiB = 220 bytes, GiB = 230 bytes, TiB = 240 bytes.
Check storage units and conversions.
1.3.2

Calculation of file size

The course focuses on calculating the uncompressed size of two kinds of digital data: bitmap images and sampled sound. Always identify what each part of the formula represents and keep track of whether your answer is in bits or bytes.

Bitmap image file size

image file size (bits) = image resolution (pixels) × colour depth (bits per pixel)

If the resolution is written as width × height, first multiply those two numbers to find the total number of pixels.

Example: 2048 × 2048 image at 16-bit colour depth

  1. Total pixels = 2048 × 2048 = 4,194,304 pixels.
  2. Bits = 4,194,304 × 16 = 67,108,864 bits.
  3. Bytes = 67,108,864 ÷ 8 = 8,388,608 bytes.
  4. MiB = 8,388,608 ÷ (1024 × 1024) = 8 MiB.
Image-size logic: more pixels or a greater colour depth means more bits must be stored. Therefore, increasing either resolution or colour depth increases the uncompressed image file size.

How many images fit in a storage device?

First calculate the size of one image in bytes. Then convert the storage capacity to the same unit and divide:

number of files = storage capacity ÷ size of one file

Only whole files can be stored, so if a calculation gives a decimal result, use the number of complete files that fit.

Sound file size

mono sound size (bits) = sample rate × sample resolution × length in seconds

For a stereo recording, there are two channels:

stereo sound size (bits) = sample rate × sample resolution × length × 2

Example: one hour of CD-style stereo audio

Sample rate = 44,100 Hz, sample resolution = 16 bits, duration = 60 minutes = 3,600 seconds, channels = 2.

  1. One channel: 44,100 × 16 × 3,600 = 2,540,160,000 bits.
  2. Stereo: 2,540,160,000 × 2 = 5,080,320,000 bits.
  3. Convert to bytes: 5,080,320,000 ÷ 8 = 635,040,000 bytes.
  4. Convert to MiB: 635,040,000 ÷ 1,048,576 ≈ 605 MiB.
Calculation checklist: write the formula, convert minutes to seconds, include the number of channels, divide bits by 8 to get bytes, then use 1024-based IEC conversions if the answer is required in KiB, MiB or GiB.
What increases?Effect on uncompressed file sizeReason
Image resolutionIncreasesMore pixels must be stored.
Colour depthIncreasesMore bits are stored for each pixel.
Sound sample rateIncreasesMore samples are stored each second.
Sound sample resolutionIncreasesMore bits are stored for each sample.
Sound durationIncreasesMore seconds of samples are stored.
Number of sound channelsIncreasesEach channel stores its own sample data.
Practise image and sound file-size calculations.
1.3.3

Data compression

Image, sound and video files can contain a large amount of data. Compression reduces the number of bits needed to represent a file.

Why compress files?

Save storage space

Smaller files use less space on storage devices such as hard disk drives and solid state drives.

Faster streaming

Reducing file size can reduce the time needed to stream music or video.

Faster transfer

Smaller files can take less time to upload, download or transfer across a network.

Use less bandwidth

Bandwidth is the maximum rate at which data can be transferred across a network, measured in bits per second. A compressed file contains fewer bits to send.

Reduce cost

Less data can mean lower storage costs, for example in cloud storage, and can reduce charges where an internet service is priced according to downloaded data.

Compression does not mean the same thing as increasing bandwidth. Compression reduces the amount of data that must be transferred. Bandwidth describes the maximum rate at which the network can transfer data.
Check why compression is used.
1.3.4

Lossy and lossless file compression

Compression methods are divided into two main groups depending on whether the original data can be recovered exactly.

Lossy compression

Some data is permanently removed. The original file cannot be reconstructed exactly after compression. This usually creates a smaller file than lossless compression.

Lossless compression

No original information is discarded. The complete original file can be reconstructed from the compressed version.

Lossy compression

A lossy algorithm decides which information can be removed while still leaving an acceptable result for its intended use. Because information is discarded, there is some loss of detail compared with the original.

MP3 and MP4

MP3 is a lossy format used for music. The course book explains that it can reduce a normal music file by about 90%. Its compression can remove sounds that are outside the human hearing range and can also remove a quieter sound when a louder sound played at the same time masks it. The book refers to this as perceptual music shaping.

MP4 is also a lossy format, but it can store multimedia rather than only sound. Music, video, photographs and animation can be included in MP4 files while keeping a size suitable for storage or streaming.

JPEG

JPEG is a lossy compression method used for bitmap images. Once information has been discarded during JPEG compression, the original raw bitmap cannot be reconstructed exactly.

The course book describes two ideas used when reducing JPEG image data:

When lossy compression is unsuitable: do not choose it when every bit of the original data must be preserved. The book gives examples such as a large complex spreadsheet or a computer application, where losing data could be disastrous.

Lossless compression and Run-Length Encoding (RLE)

Run-length encoding (RLE) is a lossless technique that works by replacing a sequence of adjacent identical values with a shorter description of that run.

run = number of repeated items + code/value of the repeated item

For text, the value can be an ASCII code. For an image, it can be a colour value. RLE works best when there are long runs of repeated data. It is less effective when values change frequently.

RLE text example

For the string aaaaabbbbccddddd:

  • five a characters → count 5 + ASCII code 97
  • four b characters → count 4 + ASCII code 98
  • two c characters → count 2 + ASCII code 99
  • five d characters → count 5 + ASCII code 100
05 97   04 98   02 99   05 100

If each original character takes one byte, the 16-byte source string is represented by eight one-byte values in this simplified example.

Flags in RLE

If the data changes very frequently, writing a count and value for every single item may give little or no saving. The book shows a refinement using a flag: the flag tells the decoder that the following values describe a repeated run. In its example, 255 is used as the flag value. Values that are not preceded by the flag can be treated directly. This avoids wasting extra run-count data where there is no useful repetition.

RLE with images

Course-book Figure 1.12 showing run-length encoding of a black-and-white letter F image
Course-book Figure 1.12: RLE applied to a black-and-white image of the letter F.

In the book's 8 × 8 black-and-white example, the original grid needs 64 bytes when each square takes one byte. The compressed RLE representation contains 30 stored values, so it needs 30 bytes in the simplified example.

Course-book Figure 1.13 showing run-length encoding of a coloured image using RGB colour values
Course-book Figure 1.13: RLE applied to a four-colour image using RGB values.

For the book's coloured 8 × 8 image, each square needs three bytes to store its RGB values, so the uncompressed data requires 8 × 8 × 3 = 192 bytes. The example's RLE data uses 92 values, giving a simplified compressed size of 92 bytes — about a 52% reduction.

Important limitation: real compressed files also contain extra information such as file headers, so the real percentage reduction may be smaller than a simplified classroom RLE calculation suggests.
FeatureLossyLossless
Can the exact original be reconstructed?NoYes
Data discarded?YesNo
Typical examples in this topicMP3, MP4, JPEGRLE
Best suited toMedia where a controlled loss of detail is acceptableData where the original must be recovered exactly
Typical file sizeOften smallerUsually larger than an equivalent lossy version
Check lossy, lossless and RLE.

Topic 1.3 revision checklist

Define bit, nibble and byte.
Use KiB, MiB, GiB, TiB, PiB and EiB.
Convert between bytes and IEC storage units.
Calculate bitmap image file size from resolution and colour depth.
Calculate mono and stereo sound file sizes.
Explain why compression is needed.
Distinguish lossy from lossless compression.
Describe MP3, MP4 and JPEG as lossy methods.
Explain how RLE stores repeated runs.
Explain when RLE is effective and why file headers affect real savings.
Ready for a mixed Topic 1.3 check?
← Topic 1.2 Text, sound and imagesComputer Science contentsTopic 2.1 Types and methods of data transmission →