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?
!Watch outCambridge 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.Example3 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.ExampleAn 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 outDo 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.ExampleA 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 outA 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.ExampleA 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.ExampleA 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 outNever 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.
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.ExampleText: 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.ExampleThe 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 outDo not say RLE always makes files smaller. With few repeats it can make them larger.