CoursesAIMathCodePhysicsAbout and FAQ

TwiddleLab / Code / How Computers Store Everything

Squeezing Without Losing

Lesson 6 of 11 · 7 min · Free, no account needed

What you will learn

  • Compression writes the same information in fewer characters.
  • Run-length encoding: a run of one colour becomes a count and a colour, like 5W.
  • Lossless: unzipping gives back exactly the original.
  • Repeats shrink well. Noise, and data that is already zipped, do not.

Start the lesson

How the lesson goes

  1. A 10 MB folder zips into 2 MB and unzips into exactly the same files. Where did 8 MB go?

    Start small: one row of 20 pixels.

  2. Write each run as a count and a colour.

    Tap a run of one colour. Then do the same with two more rows.

  3. Does the trick work on every picture?

    Zip both rows, then tap the one that got shorter.

  4. Compression without loss.

    Compression: the same information in fewer characters or bytes.

    Run-length encoding (RLE): each run of one value becomes a count and the value. 7W6K3R4W.

    Lossless: unpacking gives back exactly the original, bit for bit. ZIP and PNG are lossless; they use smarter tricks than RLE, like spotting repeated words.

  5. Be the unzipper: paint the row from its code.

    3R2G4R1B means 3 red, 2 green, 4 red, 1 blue.

  6. Zip golf: paint your own 8 x 8 icon under par.

    Each row's code is counted live on the right. Fewer colour changes, shorter code. Par is 40 characters.

  7. Which files would run-length coding shrink?

    Think: does it have long runs of the same value?

  8. Send only the code. Can the other side rebuild your icon?

    The unzipper reads the code of your icon, chip by chip.

  9. These codes have a typo. Fix them.

    Tap the wrong chip, then change its number until the rows match.

  10. Now add camera noise to your icon.

    Turn up the noise and watch the code length.

Previous lesson: Sound Is a List of Numbers · Next lesson: Throw Away What You Won't Notice · Part of the course: How Computers Store Everything

Lessons are interactive and need JavaScript. Turn it on to play them.

Loading TwiddleLab