How Digital Data Works

Req 3b — Lossy vs. Lossless Compression

3b.
Describe the difference between lossy and lossless data compression, and give an example where each might be used.

A single three-minute song stored as raw digital audio takes up about 30 megabytes. An uncompressed photo from a modern camera can be 50 megabytes or more. If we stored everything at full size, your phone would run out of space after a handful of photos and songs. Data compression solves this problem by making files smaller — and there are two fundamentally different ways to do it.

Lossless Compression: Nothing Lost

Lossless compression reduces file size without losing any data at all. When you decompress (unpack) the file, you get back the exact original — bit for bit, byte for byte.

How is this possible? Lossless compression works by finding patterns and redundancy in the data and representing them more efficiently.

Imagine you have the text: AAAAAABBBCCCCCCCCDD. Instead of storing every letter, you could write: 6A3B8C2D — same information, fewer characters. This technique is called run-length encoding, and it is one of the simplest forms of lossless compression.

Real lossless algorithms like ZIP, PNG, and FLAC use more sophisticated pattern-matching, but the principle is the same: find repeated patterns and store them using shorthand.

When to Use Lossless

Lossless compression is essential when every bit of data matters:

Lossy Compression: Good Enough Is Good Enough

Lossy compression achieves much higher compression ratios by permanently discarding some data — the parts that are least noticeable to human senses. Once the data is thrown away, it is gone forever. You cannot decompress a lossy file back to the exact original.

This sounds alarming, but it works because human perception has limits:

When to Use Lossy

Lossy compression is the right choice when perfect accuracy is not critical and small size matters:

Side by Side

FeatureLosslessLossy
Data preserved?100% — original perfectly recoverableSome data permanently removed
Compression ratioModerate (2:1 to 3:1 typical)High (10:1 to 50:1 or more)
File sizeLargerMuch smaller
Best forDocuments, code, archival, medicalStreaming, sharing, everyday media
Common formatsZIP, PNG, FLAC, GZIPJPEG, MP3, AAC, MP4, H.264
Comparison of a crisp lossless mountain photo versus the same photo with visible lossy JPEG compression artifacts

A Real-World Analogy

Think of lossless compression like packing a suitcase efficiently — you fold clothes tightly and use every inch of space, but everything you packed is still there when you arrive. Lossy compression is like deciding to leave some items at home — you travel lighter, and you might not even miss what you left behind, but you cannot get those items back until you return home.

Khan Academy — Data Compression An interactive lesson from Khan Academy explaining lossless and lossy compression with hands-on examples.

You now understand how data is both stored and compressed. Next, let’s look at the devices that process all this data — and how programming makes them useful.