Measuring storage
All data is stored as bits, but we group them into larger units. Eight bits make one byte, which can store a single character. Larger amounts are measured in kibibytes, mebibytes and gibibytes. A single high-resolution photograph can be several mebibytes, while a full-length film may be several gibibytes, so knowing these units helps you judge whether a file will fit on a device.
| Unit | Size |
|---|---|
| 1 byte | 8 bits |
| 1 kibibyte (KiB) | 1024 bytes |
| 1 mebibyte (MiB) | 1024 KiB |
| 1 gibibyte (GiB) | 1024 MiB |
Why compress data?
Compression reduces the number of bits needed to store a file. Smaller files take up less storage, download faster and use less bandwidth when sent over a network.
Key idea
Lossless compression lets the original file be rebuilt exactly. Lossy compression permanently removes some detail to save more space.
Compression methods
A common lossless technique is run-length encoding (RLE), which replaces a run of identical values with one value and a count. Lossy methods such as JPEG (images) and MP3 (sound) discard information the human eye or ear is unlikely to notice. The best choice depends on the file: losing a few subtle colours in a photo is usually acceptable, but losing a single character in a program would stop it working, so compression is used everywhere online, from streaming video to sending photos in a chat app.
Example
Using RLE, the pixels WWWWWWBBB can be stored as 6W3B, cutting nine values down to a much shorter code.
Remember
- Use lossless for text, software and spreadsheets — no data can be lost.
- Lossy suits photos, music and video, where small losses are acceptable.