Data Storage and Compression
How data storage is measured, how big image and sound files are, and two ways of making files smaller.
WATCH IT EXPLAINED
EXPLORE IT IN 3D
Pack it, size it, squeeze it
A file-transfer bench: a drive, a laptop, an LED panel and a stack of crates. Pack bits into bytes, climb the 1024 ladder and beat the upload bar — then squeeze files the lossless way and the lossy way.
Bits, nibbles and bytes
A bit is one 0 or 1; four bits make a nibble, and eight bits make a byte.
| Unit | Size | Holds |
|---|---|---|
| bit | a single 0 or 1 | the smallest unit of data |
| nibble | 4 bits | exactly one hexadecimal digit, 0 to F |
| byte | 8 bits | one ASCII character |
Why is a nibble one hex digit? Four bits have 2⁴ = 16 patterns, and hex has 16 digits.
Every larger unit of storage is counted in bytes.
The 1024 ladder
Above the byte, each unit is 1024 of the unit below it.
| Unit | Equals |
|---|---|
| 1 nibble | 4 bits |
| 1 byte | 8 bits |
| 1 kibibyte (KiB) | 1024 bytes |
| 1 mebibyte (MiB) | 1024 KiB |
| 1 gibibyte (GiB) | 1024 MiB |
| 1 tebibyte (TiB) | 1024 GiB |
| 1 pebibyte (PiB) | 1024 TiB |
| 1 exbibyte (EiB) | 1024 PiB |
- To a smaller unit: go down the ladder. Multiply by 1024 at each step, and by 8 to turn bytes into bits.
- To a bigger unit: go up the ladder and divide.
Exam:Cambridge calculations must use 1024, not 1000. Give the answer in the unit the question names.
kB or KiB?
A kilobyte (kB) is 1000 bytes, but a kibibyte (KiB) is 1024 bytes, and Cambridge uses KiB.
In everyday use, kilo means 1000. So:
| kB (kilobyte) | Kibibyte (KiB) | |
|---|---|---|
| Size | 1000 bytes | 1024 bytes |
The "bi" names were made to keep the two apart.
The gap grows at every step:
- a KiB is 2.4% more than a kB
- a GiB is about 7.4% more than a GB
- a TiB is almost 10% more than a TB
Example:A drive sold as 1 TB holds 10¹² bytes. A computer that counts in 1024s shows it as about 931 GiB.
Exam:Cambridge questions use the 1024 units.
File sizes, in the unit asked
Work out a file size in bits first, then divide your way up to the unit the question asks for.
| File | Size in bits |
|---|---|
| Image | width × height × colour depth |
| Sound | sample rate × sample resolution × length in seconds |
Then:
- Divide by 8 to get bytes.
- Divide by 1024 for each step up the ladder.
- Stop at the unit the question asks for.
Example:A 1920 × 1080 photo at 24-bit colour comes to about 5.93 MiB. So a 16 GiB memory card holds 2 761 of them.
IMAGE 1920 × 1080 pixels, 24-bit colour
bits = width × height × colour depth
= 1920 × 1080 × 24 = 49 766 400 bits
÷ 8 = 6 220 800 bytes
÷ 1024 = 6 075 KiB
÷ 1024 ≈ 5.93 MiB
SOUND 44 100 Hz, 16-bit,
3 minutes = 180 s
bits = sample rate × resolution × seconds
= 44 100 × 16 × 180
= 127 008 000 bits
÷ 8 = 15 876 000 bytes
÷ 1024 ≈ 15 503.91 KiB
÷ 1024 ≈ 15.14 MiBWhy files are compressed
Compression makes a file smaller, so it takes less space and travels faster.
Compression exists to reduce the size of a file. A smaller file:
- needs less storage space
- needs less bandwidth (network capacity) to send
- has a shorter transmission time, so it downloads, uploads and streams faster
- fits more easily inside limits, such as the largest attachment an email may carry
Example:A 16 MiB file takes about 16.78 seconds on a link of 8 000 000 bits per second. Compressed to 10% of its size, it takes about 1.68 seconds.
Lossless compression
Lossless compression makes a file smaller, and you get back exactly the original, bit for bit.
Lossless compression makes a file smaller without permanently losing any data. When the file is decompressed, it is exactly the original, bit for bit.
How? It finds patterns, such as repeated values, and writes them in a shorter way.
Use it whenever every bit matters:
- text documents
- program files
- spreadsheets
Remember:Changing even one bit could change a letter, a number or an instruction.
Run-length encoding (RLE)
RLE writes each run of repeated values as a count and the value, so nothing is lost.
Run-length encoding (RLE) replaces each run of identical values with two things:
- how many times the value repeats
- the value itself
To decode, write each value out that many times. The original comes back exactly, so RLE is lossless. Nothing is removed: the repeats are only written as a count.
Example:WWWWWWBBBWWWW (13 values) becomes 6W 3B 4W (6 values).
Watch out:RLE saves space only when there are long runs. When values rarely repeat, every single value becomes a pair. Then the encoded data is bigger than the original.
original WWWWWWBBBWWWW 13 values
encoded 6W 3B 4W 6 values
decode 6 × W, 3 × B, 4 × W
→ WWWWWWBBBWWWW identicalLossy compression
Lossy compression throws data away for good, to make a file much smaller.
Lossy compression reduces the file size by permanently removing data. The original can never be rebuilt exactly.
| File | How to make it smaller |
|---|---|
| Image | reduce the resolution or the colour depth |
| Sound | reduce the sample rate or the sample resolution |
Formats such as JPEG for photos and MP3 for music also remove detail that people are unlikely to notice.
The file can become many times smaller, at some cost in quality.
photo 4000 × 3000 pixels, 24-bit colour
= 36 000 000 bytes ≈ 34.33 MiB
half the width and half the height
= 9 000 000 bytes ≈ 8.58 MiB
(25% of the original)
then 8-bit colour instead of 24-bit
= 3 000 000 bytes ≈ 2.86 MiBChoosing lossy or lossless
If losing any data would matter, use lossless; if a small, hard-to-notice drop is fine, use lossy.
Ask one question: would losing any data matter?
| Data | Choose | Why |
|---|---|---|
| text, program code, spreadsheets | lossless compression | one wrong bit corrupts it |
| medical or legal images | lossless | losing any data would matter |
| photos on a website | lossy compression | far smaller, and the drop in quality is hard to see |
| music or video streaming | lossy | far smaller, and the drop in quality is hard to hear |
Remember:Lossy is usually the better choice when a small drop in quality is acceptable, because it can make files far smaller than lossless can.
- Width
- 800 pixels
- Height
- 600 pixels
- Colour depth
- 8 bits per pixel
KEY TERMS
CHECK YOURSELF
1.Which type of compression must be used for a program file?
2.Run-length encoding turns 'aaaabbb' into '4a3b'. Is any data lost?
3.Which of these is a lossy compression method?
4.An image is 49 766 400 bits. Which working gives its size in KiB?
5.RLE is used on the text ABCDEF, where no letter repeats. What happens?