MGU Computer Science Series — Computer Science Foundations
Part I: What Computer Science Studies
Bits · Binary · Text · Images · Audio · Compression · Limits
Chapter 4 of 28
MGU Computer Science Series  ·  Computer Science Foundations  ·  Library Textbook Edition

Chapter 4:
Data, Representation,
and Binary

How computers transform photographs, music, words, and programs into patterns of two symbols — and what happens when that transformation is imperfect

■ Part I: What Computer Science Studies ■

■ 10 Sections  •  Chapter 4 of 28 ■

Follows Chapter 3: Problem Solving and Computational Thinking  ·  Prepares students for Part II: Programming Foundations

Purpose and Overview

Chapter 4 introduces one of the most important and surprising ideas in computer science: all digital information — every photograph, every song, every word, every number, every video, every program — is stored and processed as patterns of just two symbols. This chapter builds patient, visual understanding of binary representation. Students learn bits, bytes, binary counting, decimal-to-binary conversion, text encoding, image representation, sound representation, compression, and the limits of digital representation.

By the end of the chapter, you should understand why representation matters, how computers transform the world into numbers, and what happens when that transformation is imperfect. These ideas sit beneath every file, every format, every network transmission, and every hardware design in computing.

Opening Question

Your phone holds thousands of photographs, hundreds of songs, years of text messages, and every app you have installed — all stored on a chip the size of a fingernail. How? Every one of those photographs, songs, words, and programs has been translated into a long sequence of two symbols: 0 and 1. By the end of this chapter, you will understand exactly how that translation works, why two symbols are sufficient for everything, and what is gained and lost in the process.

10Sections
12Objectives
20+Key Terms
8+Worked Examples

Learning Objectives

  1. 1Explain why representation is fundamental to all of computing.
  2. 2Define bit and byte and state the relationship between them.
  3. 3Identify place values in the binary number system.
  4. 4Convert binary numbers to decimal by summing powers of two.
  5. 5Convert decimal numbers to binary using the repeated division method.
  6. 6Describe how text is represented using ASCII and Unicode/UTF-8.
  7. 7Explain how digital images are represented as grids of pixels with RGB color values.
  8. 8Describe how sound is digitized through sampling and bit depth.
  9. 9Distinguish lossless from lossy compression and give examples of each.
  10. 10Identify overflow, floating-point approximation, and lossy compression as limits of digital representation.
  11. 11Estimate file sizes for text, images, and audio using simple calculations.
  12. 12Recognize encoding mismatches as a source of garbled text and explain why they occur.

Sections at a Glance

Sections 4.1 – 4.5
The Binary Foundation: Bits, Bytes, and Numbers
Why Representation Matters · Bits and Bytes · Binary Numbers · Counting · Conversion

Why Representation Matters

Before a computer can store, process, or transmit anything — a name, a temperature reading, a photograph, a piece of music — it must answer a fundamental question: how do we turn this into something the machine can handle?

That process of turning information into a form a machine can work with is called representation. Every choice about representation has consequences. Some choices make storage smaller. Some make processing faster. Some lose information in the translation. Some preserve everything exactly. Some work for certain kinds of information and fail for others.

The Core Problem

Consider something simple: your name. Your name is a sequence of letters. But a computer does not know what a letter is. It only knows electrical signals — voltage present or voltage absent, current flowing or current not flowing, a magnetic domain pointing one way or another. To store your name, a computer needs a way to connect the human idea of a letter to a pattern of electrical states that the machine can record and retrieve.

That connection — from human meaning to machine-readable pattern and back again — is what representation systems provide.

Everything Is a Number

One of the central choices in digital computing is to reduce everything to numbers, and then to represent those numbers in binary form. Text becomes numbers. Colors become numbers. Musical tones become numbers. Video frames become numbers. Even programs themselves — the instructions that tell the computer what to do — are stored as numbers.

This is a remarkable commitment. It means the same physical hardware that stores a piece of music can also store a photograph or a legal document. The price of this generality is that the computer itself does not know what kind of information it is working with. A file full of numbers might be music or it might be a spreadsheet. The computer relies on software, file formats, and encoding standards to tell it what the numbers mean.

Why Representation MattersThe Question It Answers
File sizes varyA song is larger than a text document because audio requires far more numbers to represent.
Images look blockyCompression systems discard information to save space; when too much is discarded the loss becomes visible.
Text displays incorrectlyIf a program uses the wrong encoding, numbers are matched to the wrong characters.
Some calculations are slightly wrongComputers store decimal fractions as binary approximations, which introduce tiny rounding errors.
Storage is measured in specific unitsBits, bytes, kilobytes, megabytes, gigabytes all reflect the binary foundation of digital storage.

Bits and Bytes

The Bit

The smallest unit of information in a digital computer is called a bit, short for binary digit. A bit can hold exactly one of two values: 0 or 1. By itself a bit is almost nothing. But bits are never used alone — they are organized into groups, and in groups they become enormously expressive.

Physical Bit Storage — Four Examples

Hard disk drive — A tiny magnetic region on a spinning disk, magnetized in one of two directions.

Solid-state drive (SSD) — A charge level stored in a flash memory cell.

RAM — A flip-flop circuit that can be set to either of two stable states.

Fiber-optic cable — A pulse of light present (1) or absent (0).

The Byte and Storage Units

Eight bits grouped together form a byte. The byte is the standard working unit for most computing purposes. Eight bits can represent 2⁸ = 256 different values — enough for all characters in simple text encoding systems. As computers handle more data, storage is measured in progressively larger units.

UnitAbbreviationApproximate Size
Bitb1 binary digit
ByteB8 bits
KilobyteKB~1,000 bytes (exactly 1,024 in binary)
MegabyteMB~1,000,000 bytes
GigabyteGB~1,000,000,000 bytes
TerabyteTB~1,000,000,000,000 bytes
PetabytePB~1,000,000,000,000,000 bytes
Worked Example 4.2.1
Estimating typical file sizes
  • Text docA plain text document of ~1,000 characters uses roughly 1,000 bytes — about 1 KB.
  • PhotoA high-quality photograph uses 3–6 MB of storage (compressed).
  • VideoOne hour of HD video uses roughly 3–10 GB of storage (compressed).
  • Hard driveA modern hard drive holds 1–4 TB — millions of photographs, thousands of hours of video.
Bits vs. Bytes — A Common Confusion

Internet speeds are advertised in bits per second (Mbps or Gbps). File sizes are measured in bytes (MB or GB). A 100 Mbps connection transfers about 12.5 MB per second, because there are 8 bits per byte. Always check whether a unit is bits (lowercase b) or bytes (uppercase B).

Sections 4.3 – 4.5
Reading, Counting, and Converting Binary
Place Values · Counting Patterns · Decimal ↔ Binary Conversion

Binary Numbers

Computers use binary — the base-2 number system — because it maps perfectly onto two-state physical hardware. The decimal system we use in everyday life is base-10: it uses ten symbols (0–9) and groups quantities in powers of ten. Binary uses base-2: it uses only two symbols (0 and 1) and groups quantities in powers of two. The logic is identical; only the base changes.

Decimal Place Values (Review)

Thousands (10³)Hundreds (10²)Tens (10¹)Ones (10⁰)Total
34723,000 + 400 + 70 + 2 = 3,472

Binary Place Values

Each position in a binary number represents a power of 2. The digit in that position tells you how many of that power's worth to count — either 0 or 1.

Diagram 4-A: 8-Bit Place Value Table
Position:   7      6      5      4      3      2      1      0
Power:     2⁷     2⁶     2⁵     2⁴     2³     2²     2¹     2⁰
Value:     128     64     32     16      8      4      2      1
Worked Example 4.3.1
Reading the binary number 1011
  • Place table | 8 (2³) | 4 (2²) | 2 (2¹) | 1 (2⁰) | | 1 | 0 | 1 | 1 |
  • Sum 1sPositions containing 1: 8 + 2 + 1 = 11
  • CheckBinary 1011 = Decimal 11. ✓
Answer: 1011₂ = 11₁₀
Worked Example 4.3.2
Reading the binary number 10110100
  • Place table | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
  • Sum 1s128 + 32 + 16 + 4 = 180
Answer: 10110100₂ = 180₁₀

Counting in Binary

DecimalBinaryDecimalBinary
0000081000
1000191001
20010101010
30011111011
40100121100
50101131101
60110141110
70111151111
Four Key Patterns in Binary Counting

Pattern 1 — The rightmost digit alternates with every step: 0, 1, 0, 1 ...

Pattern 2 — Each column changes half as often as the column to its right. The twos place changes every 2 counts. The fours place every 4. The eights place every 8.

Pattern 3 — The maximum value of n bits is 2ⁿ − 1. Four bits: 0 to 15. Eight bits: 0 to 255. Sixteen bits: 0 to 65,535.

Pattern 4 — Adding one more bit doubles the number of representable values: 4 bits → 16 values; 5 bits → 32 values.

Worked Example 4.4.1
Adding 0101 + 0011 in binary
  • Setup 0101 (decimal 5) + 0011 (decimal 3) ------
  • Ones col1 + 1 = 10. Write 0, carry 1.
  • Twos col0 + 1 + 1 (carry) = 10. Write 0, carry 1.
  • Fours col1 + 0 + 1 (carry) = 10. Write 0, carry 1.
  • Eights col0 + 0 + 1 (carry) = 1. Write 1.
  • Result 1000 (decimal 8) ✓
Answer: 0101₂ + 0011₂ = 1000₂ = 8₁₀

Decimal and Binary Conversion

Converting by hand builds intuition that a calculator cannot provide. Binary-to-decimal conversion uses the place-value table from Section 4.3. For decimal-to-binary, the most reliable method is repeated division by 2.

The Repeated Division Method

Divide the number by 2 repeatedly, collecting remainders. Read the remainders from bottom to top to obtain the binary representation.

Worked Example 4.5.1
Convert decimal 25 to binary
  • Divide 25 ÷ 2 = 12 remainder 1 12 ÷ 2 = 6 remainder 0 6 ÷ 2 = 3 remainder 0 3 ÷ 2 = 1 remainder 1 1 ÷ 2 = 0 remainder 1
  • Read upRemainders bottom to top: 1 1 0 0 1
  • Check16 + 8 + 0 + 0 + 1 = 25. ✓
Answer: 25₁₀ = 11001₂
Worked Example 4.5.2
Convert decimal 200 to binary
  • Divide 200 ÷ 2 = 100 remainder 0 100 ÷ 2 = 50 remainder 0 50 ÷ 2 = 25 remainder 0 25 ÷ 2 = 12 remainder 1 12 ÷ 2 = 6 remainder 0 6 ÷ 2 = 3 remainder 0 3 ÷ 2 = 1 remainder 1 1 ÷ 2 = 0 remainder 1
  • Read upRemainders bottom to top: 1100 1000
  • Check128 + 64 + 0 + 0 + 8 + 0 + 0 + 0 = 200. ✓
Answer: 200₁₀ = 11001000₂

Common Conversions to Memorize

DecimalBinaryNotes
10000 0001
20000 0010
40000 0100
80000 1000
160001 0000
320010 0000
640100 0000
1281000 0000
2551111 1111Maximum 8-bit value
Practice 4.1–4.5Binary Arithmetic and Conversion
  1. Decimal value of 1100 1010?
  2. Decimal value of 0001 1111?
  3. Convert 83 to binary. Show all steps.
  4. Convert 210 to binary. Show all steps.
  5. Write binary for decimal 16 through 23 (five digits each).
  6. Largest value in a 10-bit unsigned integer?
  7. Add 0011 1010 + 0001 0110 in binary. Give result in both binary and decimal.
  8. If an 8-bit unsigned integer holds 255 and you add 1, what value is stored? Explain why.
Section 4.6
Representing Text
The Encoding Problem · ASCII · Unicode · UTF-8 · Encoding Errors

Representing Text

Text is made of characters: letters, digits, punctuation, spaces, and special symbols. A computer stores only numbers. The solution is a character encoding — a standardized table that maps characters to numbers. If both the program that writes the text and the program that reads the text use the same encoding table, the text displays correctly. If they use different tables, the characters appear wrong.

ASCII

The earliest widely-used encoding for English text is ASCII (American Standard Code for Information Interchange), developed in the early 1960s. ASCII assigns numbers from 0 to 127 to 128 characters: control codes, printable characters, digits, punctuation, and both cases of all 26 English letters.

CharacterDecimalBinaryCharacterDecimalBinary
Space320010 0000a970110 0001
!330010 0001b980110 0010
0480011 0000z1220111 1010
9570011 1001A650100 0001
B660100 0010Z900101 1010
Pattern: Uppercase vs. Lowercase

Uppercase A is 65; lowercase a is 97. The difference is exactly 32 for every letter. This is intentional — it makes case conversion simple for programs. The bit at position 5 (value 32) is the only difference between a capital letter and its lowercase version.

Worked Example 4.6.1
Encoding "Hi!" in ASCII
  • CharactersH → 72, i → 105, ! → 33
  • Binary H → 0100 1000 i → 0110 1001 ! → 0010 0001
  • StorageThree bytes stored in sequence — one byte per character.
Answer: "Hi!" = 3 bytes = 0100 1000 0110 1001 0010 0001

ASCII's Limitation

ASCII was designed for American English. With only 128 values, it has no room for accented letters (é, ñ, ü), non-Latin alphabets (Arabic, Chinese, Cyrillic, Hindi), mathematical symbols, or emoji. As computing became global, this limitation became a serious problem.

Unicode and UTF-8

Unicode is a modern standard that assigns a unique number (called a code point) to every character in virtually every writing system on Earth — and to emoji, mathematical symbols, musical notation, and much more. As of recent editions, Unicode covers over 140,000 characters across 150+ scripts.

UTF-8 is the most common way of encoding Unicode text as bytes. It is backward-compatible with ASCII: the first 128 Unicode code points use the same numbers as ASCII and store in exactly one byte each. Characters outside the ASCII range use 2, 3, or 4 bytes. This is why most English text files remain small and fast to process in UTF-8 — they use one byte per character — while Chinese or Arabic text uses more bytes per character.

UTF-8 is the dominant encoding for the web, for most programming languages, and for most modern software. Encoding mismatches produce garbled text — question marks, boxes, or strange characters where letters should appear.

Practice 4.6Character Encoding
  1. Write the ASCII decimal value and 8-bit binary for each character in the word "Cat".
  2. A file contains byte values 72, 101, 108, 108, 111. Using ASCII, what word do these bytes encode?
  3. Why does a file of English text saved in UTF-8 have the same byte count as the same file in ASCII (assuming no special characters)?
  4. A student opens a file saved with one encoding in software that expects a different encoding. Describe what the student will likely see and explain why.
  5. Uppercase A is ASCII 65. What is the ASCII value of lowercase a? Without looking it up, determine the ASCII value of lowercase b. Explain your reasoning.
Section 4.7
Representing Images
Pixels · Resolution · RGB Color · 24-Bit True Color · File Size · Alpha Channels

Representing Images

A digital image is made of pixels — tiny colored squares arranged in a rectangular grid. (Pixel is short for "picture element.") If you zoom into a digital photograph far enough, you will see the individual pixels clearly. What looks like a smooth curve in a photograph is actually many tiny squares of slightly different colors arranged to approximate that curve.

An image's resolution is the number of pixels in its grid, expressed as width × height. A common display resolution is 1920 × 1080 pixels. A high-resolution smartphone photograph might be 4000 × 3000 pixels or larger.

RGB Color

The most common system for representing color digitally is RGB — Red, Green, Blue. Every pixel's color is described as a mixture of three components, each stored as an 8-bit number (0 to 255). This gives 256 × 256 × 256 = 16,777,216 possible colors per pixel. Three bytes per pixel is called 24-bit color or true color.

Diagram 4-C: RGB Color Model
Each pixel stores three values:

      RED         GREEN        BLUE
   [0 to 255]  [0 to 255]  [0 to 255]
       |             |            |
       └─────────────┴────────────┘
                     |
              = 1 color per pixel

Examples:
  (255,   0,   0)  →  Pure red
  (  0, 255,   0)  →  Pure green
  (  0,   0, 255)  →  Pure blue
  (255, 255,   0)  →  Yellow (red + green)
  (255, 165,   0)  →  Orange
  (  0,   0,   0)  →  Black
  (255, 255, 255)  →  White
  (128, 128, 128)  →  Medium gray
ColorRedGreenBlue
Pure red25500
Pure green02550
Pure blue00255
White255255255
Black000
Yellow2552550
Orange2551650
50% gray128128128
Worked Example 4.7.1
Calculating uncompressed image file size
  • FormulaFile size = width × height × 3 bytes
  • 1080p image 1920 × 1080 × 3 = 6,220,800 bytes ≈ 6.2 MB
  • 12 MP camera 12,000,000 × 3 = 36,000,000 bytes = 36 MB (uncompressed)
  • JPEG versionThe same 12 MP photo saved as JPEG is typically 3–8 MB — compression reduces it by a factor of 5 to 12.
Uncompressed size = pixels × 3. Compression dramatically reduces this.
Alpha Channels and Transparency

Some formats store a fourth value per pixel — alpha — representing transparency. Alpha 0 = fully transparent; alpha 255 = fully opaque. PNG supports alpha channels, which is why it is used for logos and graphics with transparent backgrounds. JPEG does not support transparency, which is why JPEG images always have a solid background.

Practice 4.7Images and Color
  1. An image is 800 × 600 pixels, 24-bit RGB. What is its uncompressed size in bytes? In megabytes?
  2. Write RGB values for: (a) pure cyan (equal green and blue, no red); (b) dark gray at about 25% brightness; (c) pure magenta (maximum red and blue, no green).
  3. Explain why a PNG of the same photograph is usually larger than a JPEG, even when both look identical on screen.
  4. A logo uses flat areas of color with sharp edges. PNG or JPEG? Explain your reasoning using compression concepts from this chapter.
  5. An image is resized from 3000 × 2000 to 300 × 200. What fraction of the original pixel data is retained? What happens to detail in the original?
Section 4.8
Representing Sound
Analog vs. Digital · Sampling · Sampling Rate · Bit Depth · Nyquist Theorem

Representing Sound

Sound is vibrations traveling through air, creating pressure waves that our ears detect. A microphone converts those pressure waves into a continuous electrical signal — a changing voltage that mirrors the shape of the sound wave. A continuous signal is called an analog signal. To store sound digitally, we must convert that continuous analog signal into a sequence of discrete numbers. This process is called analog-to-digital conversion (ADC).

Sampling

The key technique is sampling: measuring the amplitude (height) of the sound wave at regular time intervals and storing each measurement as a number. The sampling rate is how many samples are taken per second, measured in hertz (Hz) or kilohertz (kHz). The bit depth is how many bits store each sample — more bits mean more possible amplitude levels and finer detail.

Diagram 4-B: From Sound Wave to Digital Audio
ANALOG SOUND WAVE
  │     /\      /\          /\
Amp│    /  \    /  \        /  \      /\
  │   /    \  /    \      /    \    /  \    /\
──│──/──────\/──────\────/──────\──/────\──/──\──
  │                  \  /        \/      \/    \/
  └──────────────────────────────────────────────
                    Time →

         SAMPLING — measure amplitude at regular intervals
              ↓         ↓         ↓         ↓
          Value:  42   180   255   201   87 ...

STORED AS NUMBERS: 42, 180, 255, 201, 87 ...

PLAYBACK: Numbers → DAC → Reconstructed wave → Speaker → Sound
Use CaseSampling RateBit DepthQuality
Telephone8,000 Hz8-bitVoice intelligibility only
CD audio44,100 Hz16-bitFull human hearing range
Professional audio48,000–96,000 Hz24-bitStudio/film quality
Worked Example 4.8.1
Calculating CD-quality audio file size
  • Formula Size = sampling rate × (bit depth ÷ 8) × channels × seconds
  • One minute, stereo 44,100 × (16 ÷ 8) × 2 × 60 = 44,100 × 2 × 2 × 60 = 10,584,000 bytes ≈ 10.6 MB per minute
  • ContextA CD holds ~74 minutes in ~650 MB. MP3 compression reduces this to roughly 1 MB per minute.
One minute of CD-quality stereo audio ≈ 10.6 MB uncompressed.
The Nyquist Theorem

To accurately capture a sound frequency, the sampling rate must be at least twice that frequency. Human hearing extends to roughly 20,000 Hz. CD audio samples at 44,100 Hz — more than twice 20,000 — which is why CD audio captures everything the human ear can hear.

Practice 4.8Audio Representation
  1. A recording is made at 44,100 Hz sampling rate, 16-bit depth, mono (1 channel). How many bytes does one second require? Show your calculation.
  2. How does the Nyquist theorem explain why CD audio (44,100 Hz) is sufficient for human hearing?
  3. Why does telephone audio (8,000 Hz) sound different from live sound? What information is lost?
Section 4.9
Compression
Why Compression · Lossless · Run-Length Encoding · Lossy JPEG and MP3 · Tradeoffs

Compression

Representing images, audio, and video using the methods described above produces very large files. A one-hour uncompressed HD video might require 500 GB or more. Compression reduces file size by finding smarter ways to represent data — using fewer bits without losing important information, or while accepting controlled losses.

Lossless Compression

Lossless compression shrinks a file so that when it is decompressed, the result is bit-for-bit identical to the original. Run-length encoding is one of the simplest techniques: instead of repeating the same value many times, the compressed file stores the value once with a count.

Worked Example 4.9.1
Run-length encoding
  • ProblemA row of pixels: 40 white, then 12 black, then 48 white. Storing every pixel individually requires 100 values.
  • Encoded (white, 40) (black, 12) (white, 48)
  • ResultThree pairs instead of 100 individual values. Dramatic saving for images with large flat areas.
Run-length encoding replaces repetition with (value, count) pairs.

Other lossless methods include dictionary compression (used in ZIP files), which finds repeated patterns and replaces them with short codes, and Huffman coding, which assigns shorter bit sequences to more frequent values. PNG images and ZIP files both use lossless compression. Text compression is always lossless — you cannot afford to lose any characters from a document.

Lossy Compression

Lossy compression achieves much higher compression ratios by discarding some information permanently. The decompressed file is not identical to the original — but if the compression is well-designed, the differences are difficult or impossible for humans to perceive.

JPEG exploits the fact that the human visual system is more sensitive to brightness than to color, and more sensitive to large-scale patterns than to fine high-frequency detail. It divides the image into 8×8 blocks, applies a mathematical transform to each block, rounds away the high-frequency detail, and compresses the result. At high quality settings, losses are nearly invisible. At low quality, the 8×8 block structure becomes visible — the "blockiness" or "JPEG artifacts" you may have noticed in heavily compressed images.

MP3 uses a similar strategy for audio, discarding sounds masked by louder simultaneous sounds, very quiet sounds, and sounds at the extremes of the audible frequency range.

Diagram 4-D: Lossless vs. Lossy Compression
ORIGINAL DATA
     │
     ├──→ LOSSLESS ──→ Compressed file
     │         │              │
     │         └──────────── DECOMPRESS ──→ Exact original restored
     │
     └──→ LOSSY ──→ Compressed file (much smaller)
               │              │
               └──────────── DECOMPRESS ──→ Approximate original
                                            (some detail permanently lost)
FactorLosslessLossy
Information preserved100%Some lost permanently
Typical compression ratioModestHigh
Suitable for text/codeYesNever
Suitable for photographsYes (PNG, TIFF)Yes (JPEG), if quality acceptable
Suitable for audioYes (FLAC)Yes (MP3), if quality acceptable
Can compress further?YesYes, but quality degrades further
Repeated Lossy Saves

A file that has already been lossy-compressed will lose additional quality if compressed again at lower quality. Repeatedly saving JPEG images or re-encoding MP3 files degrades their quality each time, because each pass discards more information permanently.

Practice 4.9Compression
  1. Explain the difference between lossless and lossy compression. Give one example of each.
  2. A student repeatedly saves an image as JPEG each time they make a small edit, rather than keeping the original and exporting JPEG only for final delivery. What problem might result?
  3. Why is lossless compression always used for text files and program files, but lossy compression is considered acceptable for photographs?
  4. A satellite image is compressed for transmission. Describe a situation where lossy compression would be completely unacceptable for this image, and explain why.
Section 4.10
Limits, Errors, and Approximation
Overflow · Floating-Point · Sampling Limits · Encoding Errors · Summary

Limits, Errors, and Approximation in Digital Representation

Digital representation is powerful, but it is not perfect. Every representation system has limits, and understanding those limits is part of working carefully with computers.

Overflow

Each numeric type has a maximum value. If a calculation tries to exceed that maximum, the result wraps around or triggers an error — a problem called overflow. An 8-bit unsigned integer can hold values from 0 to 255. If you add 1 to 255 in a system without overflow protection, the result is 0, not 256.

Worked Example 4.10.1
8-bit overflow
  • Setup 255 = 1111 1111 (8 bits) + 1 = 0000 0001 ───────────────── 256 = 1 0000 0000 ← 9 bits
  • StorageOnly 8 bits are stored. The leading 1 is discarded.
  • Result Stored: 0000 0000 = 0
Adding 1 to the maximum 8-bit value produces 0, not 256. This is integer overflow.
Real-World Overflow: Ariane 5, 1996

The Ariane 5 rocket was destroyed shortly after launch because of an overflow error. A value that was safely within range in the Ariane 4 software exceeded the representable range in the Ariane 5's different hardware, causing the flight computer to crash and the rocket to self-destruct. Overflow errors have real consequences.

Floating-Point Approximation

Computers typically represent real numbers using floating-point representation. Floating-point numbers are stored in binary, which means they can represent fractions whose denominators are powers of 2 — like 1/2, 1/4, 1/8 — exactly. But fractions whose denominators are not powers of 2 — like 1/10 — cannot be stored exactly in binary, just as 1/3 cannot be stored exactly in decimal.

Worked Example 4.10.2
Floating-point arithmetic in Python
  • Expected0.1 + 0.2 should equal 0.3
  • Actual0.1 + 0.2 = 0.30000000000000004
  • WhyNeither 0.1 nor 0.2 has an exact binary representation. The tiny approximation errors accumulate in the addition.
  • ConsequenceThe comparison 0.1 + 0.2 == 0.3 may evaluate to False in most programming languages.
Floating-point arithmetic introduces tiny approximation errors. Financial software uses exact decimal arithmetic to avoid this.

Other Limits

Sampling limits: Digital audio captures sound at a finite number of samples per second. Frequencies above half the sampling rate cannot be represented accurately — they produce aliasing, where high-frequency content appears at incorrect lower frequencies. Similarly, fine image detail smaller than one pixel is simply lost.

Encoding errors: If a file is saved with one encoding (say, UTF-8) but opened by software expecting a different encoding (say, Windows-1252), the bytes are matched to the wrong characters, producing garbled text. Text files should always be labeled with their encoding.

Lossy compression artifacts: Once an image has been saved as JPEG at low quality, the discarded detail cannot be recovered. Medical imaging, scientific photography, and archival records should not use lossy compression.

Strength of Digital RepresentationCorresponding Limitation
Exact storage of values within rangeCannot represent values outside the range (overflow)
Perfect copying and transmissionLossy compression permanently discards information
Consistent encoding standardsEncoding mismatches cause garbled text
High precision with many bitsFloating-point introduces tiny approximation errors
Compression reduces storage enormouslyCompression artifacts can degrade quality
Practice 4.10Limits and Errors
  1. A programmer tracks warehouse inventory with an 8-bit unsigned integer. What is the maximum item count it can store? What happens when the count reaches 255 and one more item is added?
  2. Why does 0.1 + 0.2 not equal exactly 0.3 in floating-point arithmetic? Why does this matter for financial calculations?
  3. A medical imaging system stores MRI scans. Should it use lossy compression, lossless compression, or no compression? Explain your reasoning.

Chapter Summary

Key Terms Review

bit
The smallest unit of digital information. Holds exactly one of two values: 0 or 1.
byte
A group of eight bits. The standard unit of storage in most computing contexts. Can represent 256 different values (2⁸).
binary
The base-2 number system, using only the digits 0 and 1. Used internally by all digital computers.
representation
The process of encoding information (text, images, sound) in a form a computer can store and process.
ASCII
American Standard Code for Information Interchange. A 7-bit encoding of 128 characters for American English, developed in the 1960s.
Unicode
A universal standard assigning unique numbers to every character in every writing system on Earth. Over 140,000 characters across 150+ scripts.
UTF-8
The dominant variable-length encoding for Unicode text. Backward-compatible with ASCII. Used on most of the web.
code point
The number assigned to a specific character in Unicode.
character encoding
A standardized mapping between characters and numeric values that allows computers to represent text.
pixel
The smallest unit of a digital image. A tiny colored square in a rectangular grid.
resolution
The number of pixels in a digital image, expressed as width × height (e.g., 1920 × 1080).
RGB
Red, Green, Blue. The standard system for representing pixel colors using three values (0–255 each) for the intensity of each color channel.
alpha channel
A fourth per-pixel value representing transparency, used alongside RGB in formats like PNG.
analog signal
A continuous signal that varies smoothly, such as a sound wave or the output of a microphone.
sampling
Measuring the amplitude of an analog signal at regular intervals to produce a sequence of digital values.
sampling rate
The number of samples taken per second during digitization, measured in Hz or kHz. CD audio: 44,100 Hz.
bit depth
The number of bits used to store each audio sample. Higher bit depth means more amplitude levels and less quantization error.
compression
A technique for reducing the number of bits needed to represent data, either losslessly or with acceptable losses.
lossless compression
Compression from which the original data can be perfectly reconstructed. Used for text, code, and archival purposes.
lossy compression
Compression that permanently discards some data for higher compression ratios. Used for photographs (JPEG) and audio (MP3).
run-length encoding
A lossless compression technique that replaces runs of repeated values with a (value, count) pair.
overflow
An error that occurs when a calculated value exceeds the maximum representable value for the bit width being used.
floating-point
A binary method for representing real numbers. Cannot exactly represent many decimal fractions, introducing tiny approximation errors.
aliasing
The distortion that results when a signal is sampled too slowly to represent its high-frequency content accurately.

Diagram Review

Diagram Review 4-1: Complete 8-Bit Place Value Reference
Position:   7      6      5      4      3      2      1      0
Power:     2⁷     2⁶     2⁵     2⁴     2³     2²     2¹     2⁰
Value:     128     64     32     16      8      4      2      1

To read binary → sum values at positions containing 1
To write binary → find powers of 2 that sum to target (subtraction method)
                  OR divide by 2 repeatedly, read remainders upward
Diagram Review 4-2: Data Type → Representation Method
TEXT     →  Character encoding (ASCII / UTF-8)
            Each character mapped to a number → stored as bytes

IMAGE    →  Grid of pixels × RGB bytes per pixel
            Uncompressed: width × height × 3 bytes
            JPEG: lossy compression removes high-frequency detail
            PNG:  lossless compression, supports transparency

AUDIO    →  Sampling at regular intervals
            File size = rate × (depth ÷ 8) × channels × seconds
            MP3: lossy, ~1 MB/min    FLAC: lossless, ~30–35 MB/min

PROGRAM  →  Machine instructions encoded as numbers
            Same binary format as everything else

Concept Check

Answer in your own words before attempting the longer practice questions.

  1. What is the difference between a bit and a byte?
  2. What does the place value of each position in a binary number represent?
  3. Why can a single byte represent 256 different values?
  4. What problem does a character encoding standard like ASCII or UTF-8 solve?
  5. What does "lossy" mean in lossy compression?
  6. Why does 0.1 + 0.2 not equal exactly 0.3 in floating-point arithmetic?
  7. What is a pixel, and how does resolution relate to it?
  8. What is sampling rate, and why does it matter for digital audio quality?

Practice Questions

Part ABinary Arithmetic and Conversion
  1. Decimal value of 1100 1010?
  2. Decimal value of 0001 1111?
  3. Convert 83 to binary. Show all steps.
  4. Convert 210 to binary. Show all steps.
  5. Write binary for 16 through 23 (five digits each).
  6. Largest value in a 10-bit unsigned integer?
  7. Add 0011 1010 + 0001 0110 in binary; give result in binary and decimal.
  8. An 8-bit counter holds 255; add 1 without overflow protection. What is stored? Why?
Part BCharacter Encoding
  1. Write ASCII decimal value and 8-bit binary for each character in "Cat".
  2. Byte values 72, 101, 108, 108, 111 — what word do they encode in ASCII?
  3. Why does a UTF-8 file of English text have the same byte count as an ASCII file of the same text?
  4. A file saved in UTF-8 is opened by software expecting Windows-1252. Describe what happens on screen and explain why.
  5. Uppercase A = ASCII 65. What is lowercase a? What is lowercase b? Explain your reasoning without a reference table.
Part CImages and Color
  1. Uncompressed file size of an 800 × 600 pixel, 24-bit RGB image in bytes and megabytes?
  2. Write RGB values for: (a) pure cyan; (b) dark gray at ~25% brightness; (c) pure magenta.
  3. Why is a PNG file of the same photograph usually larger than a JPEG, even when both look identical on screen?
  4. A logo uses flat areas of color with sharp edges. PNG or JPEG? Explain using compression concepts.
  5. An image is resized from 3000 × 2000 to 300 × 200. What fraction of original pixel data is retained? What happens to the lost detail?
Part DAudio and Compression
  1. 44,100 Hz, 16-bit, mono recording: how many bytes does one second require? Show the calculation.
  2. How does the Nyquist theorem explain why CD audio (44,100 Hz) is sufficient for human hearing?
  3. Explain lossless vs. lossy compression. Give one example of each and describe a use case for each.
  4. A student saves a JPEG image repeatedly through many editing sessions. What problem results? Why?
  5. Why is lossless compression always used for text files but lossy is acceptable for photographs?

Debugging and Error Analysis

Each problem contains an error. Find it, explain it, and correct it.

Error Analysis 4-1
Binary 1100 1010 → decimal: Student adds positions with 0s: 64 + 16 + 4 + 2 = 86

What is the correct approach? What is the actual decimal value?

Error Analysis 4-2
Converting decimal 95 to binary: Remainders read top to bottom: 0 1 1 1 0 1 1 Answer: 0111011

What rule did the student get backwards? What is the correct binary representation of 95?

Error Analysis 4-3
Uncompressed file size of a 1024 × 768 image: 1024 + 768 = 1792 bytes ≈ 1.8 KB

What is wrong with the student's calculation method? What is the correct uncompressed size (24-bit RGB)?

Error Analysis 4-4
Student claims: "I can compress a ZIP file with JPEG to get smaller file AND no quality loss — best of both worlds."

Identify at least two errors in this reasoning.

Error Analysis 4-5
A developer stores elapsed time in milliseconds in an 8-bit integer to time events up to 1 minute long.

How many milliseconds are in one minute? What is the maximum 8-bit value? Why will this application fail?

Design and Application Questions

  1. Design 4-1: Encoding a Secret Message. Encode the word "Code" as 8-bit ASCII binary values. Then write the bytes as decimal values. Check your work by decoding your own result.
  2. Design 4-2: Audio Quality Choices. For each use case, describe the sampling rate, bit depth, and compression approach you would recommend and explain your reasoning: (a) archiving a concert for a music library; (b) sending a short voice memo over a low-bandwidth connection; (c) recording dialogue for a film.
  3. Design 4-3: Storage Estimation. A school district wants to digitize 10,000 student records. Each record includes a photograph (800 × 600 pixels, JPEG at ~3 bytes per pixel effective after compression) and a one-page scanned document (2480 × 3508 pixels, 1 bit per pixel, lossless compression at 10:1). Estimate the total storage required. Show all calculations.
  4. Design 4-4: Encoding Design Reflection. List three specific limitations of ASCII that became problems as computing spread globally. Explain how Unicode addresses each limitation.

Real-World Applications

  1. Photography and storage. A professional photographer shoots 500 RAW (uncompressed) photos at 24 megapixels each. Estimate the total storage required. Why might a photographer prefer RAW files over JPEGs for professional work, even though the files are much larger?
  2. Medical imaging. An MRI scanner produces 3D images at 256 × 256 × 128 voxels (3D pixels), each stored as a 16-bit value. Calculate the uncompressed size of one MRI scan. Why should hospitals use lossless or no compression for diagnostic images?
  3. Streaming audio. A music streaming service serves 50 million listeners at 128 kbps (kilobits per second). Estimate the total bandwidth in terabits per second the service must provide. Show your calculation.
  4. Color depth and display. Early home computers used 4-bit color (16 colors) or 8-bit color (256 colors). How many more colors does 24-bit true color provide compared to 8-bit? Why do more colors matter for photographs but matter less for simple pixel-art games?
  5. Computing and representation together. Chapter 1 described computer science as the study of computation, information, algorithms, programs, machines, systems, networks, data, automation, and responsible technology. Connect binary representation to at least three of these areas in your own words.

Mixed Review

These questions connect Chapter 4 to ideas from Chapters 1–3.

  1. (Chapters 1 + 4) Explain how binary representation makes it possible for a single machine to store and process text, images, audio, and programs — all using the same physical hardware.
  2. (Chapters 3 + 4) Decomposition is a key computational thinking strategy from Chapter 3. How does the RGB color model apply decomposition to the problem of representing color?
  3. (Chapters 3 + 4) Abstraction is another key strategy from Chapter 3. How do character encoding standards like ASCII and UTF-8 act as abstractions? What detail do they hide from programmers, and what is the benefit?
  4. A classmate says: "My phone has 128 GB of storage, so I can store 128 billion bytes." Is this exactly right? What is the actual number of bytes in 128 GB, and why is there a difference between the two figures?

Chapter 4 Checkpoint

Chapter 4 Checkpoint
Complete without assistance — assesses all key skills and concepts from Chapter 4
Section 1 — Vocabulary and Concepts
  1. Define bit and byte. What is the relationship between them?
  2. What is the difference between lossless and lossy compression? Give one file format example of each.
  3. What is overflow? Give a brief example using 8-bit integers.
Section 2 — Binary Skills
  1. Convert binary 1010 1100 to decimal. Show your work.
  2. Convert decimal 77 to binary using repeated division. Show all steps.
  3. What is the maximum value representable by a 12-bit unsigned integer?
  4. Add 0011 + 0101 in binary. Give the result in binary and decimal.
Section 3 — Encoding and Representation
  1. Write the ASCII decimal value and binary for each character in "Go".
  2. What RGB values produce pure yellow? What produces medium gray?
  3. Calculate the uncompressed file size of a 640 × 480 pixel, 24-bit RGB image in kilobytes.
Section 4 — Analysis and Application
  1. A sound file is recorded at 22,050 Hz, 16-bit, stereo. How many bytes does 10 seconds of audio require? Show your calculation.
  2. A student claims the highest frequency that 22,050 Hz sampling can accurately capture is 44,100 Hz. Is this correct? Explain using the Nyquist theorem.
  3. A developer saves medical diagnostic images as JPEG at 60% quality to save storage space. Explain why this is problematic.
  4. Explain in your own words why 0.1 + 0.2 does not equal exactly 0.3 in floating-point arithmetic, and describe one situation where this matters.

Key Terms — Quick Reference

TermDefinition
bitSmallest unit of digital info: 0 or 1
byte8 bits; can represent 256 values
binaryBase-2 number system using only 0 and 1
ASCII128-character English encoding, 7 bits
UnicodeUniversal standard for all writing systems
UTF-8Variable-length Unicode encoding; dominant on the web
pixelSmallest unit of a digital image
RGBRed, Green, Blue color model; 3 bytes per pixel
samplingMeasuring an analog signal at regular intervals
sampling rateSamples per second; CD = 44,100 Hz
losslessCompression with perfect reconstruction
lossyCompression that permanently discards data
overflowValue exceeds maximum representable in bit width
floating-pointBinary approximation of real numbers
aliasingDistortion from undersampling high frequencies
Looking Ahead
Chapter 5: Algorithms and Instructions

Chapter 4 explained how computers represent information — all of it, ultimately, as patterns of 0s and 1s. Chapter 5 takes the next step: how does the computer decide what to do with that information? Algorithms are precise, step-by-step procedures for processing information and producing results. Chapter 5 introduces the concept of an algorithm, shows how algorithms can be written as pseudocode or drawn as flowcharts, and builds the skills of designing, tracing, and evaluating algorithms by hand. The programming strand of this textbook begins in earnest in Chapter 5 and continues through Chapter 10.

MGU Library Connection
  • Appendix B of this textbook: Binary and Data Representation Reference
  • Appendix A of this textbook: Mathematics and Logic for Computer Science
  • Later in this book — Chapter 12: Searching, Sorting, and Efficiency (Big-O applies to representation and compression)
  • Later in this book — Chapter 15: Hardware and Computer Architecture (physical storage of bits)
  • Later in this book — Chapter 18: Databases and Information Systems (data types and storage)
  • MGU Python Programming Foundations — Chapter 3: Numbers and Strings in Python
  • MGU Data Science Foundations — Chapter 2: Data Types and Storage