Intro to Computer Science / Unit 2: Binary and Data Representation
2.5 Data Compression
Notes
Compression makes files smaller so they use less storage and are faster to send.
- Lossless compression keeps all the original data, so the file can be restored exactly (ZIP files, PNG images).
- Lossy compression throws away some data that people may not notice, to make the file much smaller (JPEG images, MP3 sound). The original cannot be fully restored.
- Run-length encoding replaces repeats with a count: "AAAABBB" becomes "4A3B".
- Example: "WWWWWWWWWWBB" (10 W and 2 B) becomes "10W2B".
- Use lossless for text and programs, where every bit matters.
Practice
What is "AAABBBBCC" after run-length encoding?
There are 3 As, 4 Bs and 2 Cs.
Which kind of compression can be reversed to get the exact original file?
Lossless compression keeps all the information.
A programmer wants to compress a text document of source code before emailing it. Which type should be used and why?
Source code must be restored exactly, so lossless is required.
Ready for more? Take the Unit 2 test.