Data representation · Current focus

Bits need a meaning

Five 55-minute lessons: numbers, text, images, sound, video and compression.

2 Nov 2026 · 275 minutes

For this lesson

Review: signed binary and basic logic gates

We have covered signed binary, including eight-bit two’s complement, and the AND, OR and NOT gates.

Use these resources to review negative numbers in one byte and practise with the gate lab. BCD, Gray code, Excess-N, fixed-point and floating-point are a separate catch-up sequence.

Review: signed binary and basic logic gates

Week of November 16–20 · 50 minutes. Due Monday, 23 November, at the start of the next CS class. Practise eight-bit two’s complement and AND, OR and NOT, then revisit searching, sorting, Big O and recursion.

Earlier arithmetic review

Visual walkthrough

Aligned binary columns

Trace the stages from left to right.

  1. Weights: 8 | 4 | 2 | 1
  2. Addition: 1 + 1 = 10
  3. Borrow: 10 − 1 = 1
  4. Shift: 0101 << 1 = 1010
Discuss before continuing

Use counters for each column. Explain a carry, a borrow and a shifted partial product using new numbers.

Binary columns are worth 1, 2, 4, 8 and so on. Keep columns aligned. Addition carries; subtraction borrows; multiplication shifts each partial product by its bit position.

Try before revealing

0101 + 0011 · 0110 − 0011 · 0101 × 0011

Reveal example answer

Addition: write 0 and carry 1 when a column totals binary 10; the result is 1000. Subtraction: borrow where needed to get 0011. Multiplication: add 0101 and the shifted partial product 1010 to get 1111.

Decimal checks: 5 + 3 = 8; 6 − 3 = 3; 5 × 3 = 15.

Explain one carry, one borrow and one shift to a partner. Then complete the separate arithmetic homework, showing your own working.

Earlier reading references · text and bitmap images

Preview reading · about 5 minutes

Baumgarten, Ganea and Turland, Computer Science for the IB Diploma Programme (Hachette Learning, 2025), A1.2.2 How binary is used to store data. Page numbers below are printed book pages.

Before Text and UTF-8: read Characters and strings / ASCII encoding on page 31, Unicode encoding on page 32, and the UTF comparison table and opening UTF-8 explanation on page 34. Skip the coding exercises and detailed bit-prefix examples for this preview.

Bring one question: how is a code point different from the bytes used to store it? Stop after about five minutes; this is preview reading, not another worksheet.

After Text and UTF-8, before Bitmap images: read Images / Bitmap images on page 37. Identify pixel, colour depth and the raw-storage formula. Do not complete the later exercises yet.

By the end of this lesson

  • Explain how a bit pattern gets its meaning from an agreed representation.

Work through the lesson

01 · 55 minutes

Binary and hexadecimal

Optional reference · conversions already covered

Student workbook page 6 · Open lesson worksheet (Word)

Visual walkthrough

One byte has eight positions

Trace the stages from left to right.

  1. 128 | 64 | 32 | 16
  2. 8 | 4 | 2 | 1
  3. 00000000 → 0
  4. 11111111 → 255
Discuss before continuing

Predict 255 + 1. Python integers can grow; an unsigned eight-bit field cannot hold 256.

Start · 5 minutes

What does a carry mean in base two? Convert 101 to decimal using place values.

Explain and demonstrate · 10 minutes

Each place is worth twice the place to its right. A byte has eight bits. Unsigned means no negative values: 00000000 is 0 and 11111111 is 255. Hexadecimal groups four binary bits into one digit.

Demonstration

Draw columns 128, 64, 32, 16, 8, 4, 2, 1. Place 00101101 under them and add only the columns containing 1. Split the same byte into 0010 and 1101; use the four-bit values to get 2 and D. In Python, int("00101101", 2) checks decimal, while format(45, "08b") and format(45, "02X") check both representations. Python integers are not limited to one byte; to_bytes adds that constraint.

value = 255
print(value.to_bytes(1, "big"))

Class example · 10 minutes

Convert 00101101 to decimal and hexadecimal. Can 256 fit in one unsigned byte?

Reveal example answer

00101101 = 32 + 8 + 4 + 1 = 45. Group 0010 and 1101 to get 2D. One unsigned byte ends at 255, so 256 needs more than eight bits. In Python, (256).to_bytes(1, "big") raises OverflowError.

Your work · 25 minutes

Review place value and conversions. Use the number-systems studio. Find the limits of an unsigned byte. In Python, try converting values just below, at and above each boundary into one unsigned byte. Predict first, run, then explain any error.

Practice steps

Convert 00110110 and 10100101 using a place-value table and grouped nibbles. Predict what (-1), 0, 255 and 256 do with .to_bytes(1, "big"). Run one at a time, record the exact result or error and explain the boundary.

Check and finish · 5 minutes

Explain why the same bit pattern needs an agreed meaning. Save one predicted and actual Python boundary result.

Python practice · two short prompts

Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.

A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.

Prompt 1

Predict the binary and hexadecimal forms of 45. Change value to 54 and check your place-value work.

No imports needed.

Hint and starter code

bin() and hex() return strings with 0b and 0x prefixes.

# bin() and hex() return strings with 0b and 0x prefixes.
value = 45
print(bin(value))
print(hex(value))
Open this starter in Python playground →
Prompt 2

Complete the condition that tells whether each number fits in one unsigned byte. Test both boundaries.

No imports needed.

Hint and starter code

Use 0 <= value <= 255. A condition produces True or False.

# Use 0 <= value <= 255.
for value in [-1, 0, 255, 256]:
    fits = False  # Your turn: replace with a condition
    print(value, fits)
Open this starter in Python playground →
02 · 55 minutes

Text and UTF-8

Earlier sequence resource · revisit as needed

Student workbook page 7 · Open lesson worksheet (Word)

Prerequisite check · 3 minutes

A byte has eight bits. What is the largest unsigned value it can hold? If this is new, add 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 to get 255. Python integers can grow beyond one byte. Use the first three minutes of the opening activity for this check.

Visual walkthrough

Text is encoded as bytes

Trace the stages from left to right.

  1. A → U+0041
  2. UTF-8 → 41 hex
  3. 中 → U+4E2D
  4. UTF-8 → E4 B8 AD hex
Discuss before continuing

Both examples contain one character. Why do their UTF-8 byte counts differ? Test with encode in Python.

Start · 5 minutes

Can one visible character use more than one byte? Predict before testing.

Explain and demonstrate · 10 minutes

Unicode assigns a number, called a code point, to a character. An encoding converts code points to bytes. UTF-8 (Unicode Transformation Format, 8-bit) is an encoding that uses one to four bytes for a code point; len(text) and len(text.encode("utf-8")) answer different questions.

Demonstration

Use the code below with A, then 中, then A中. Read each output label: code points in the string, bytes after encoding, decoded text. The byte sequence needs the same encoding to recover the text. A displayed character may combine multiple code points, so len(text) is not a universal count of visible symbols.

text = "A中"
data = text.encode("utf-8")
print("code points:", len(text))
print("bytes:", len(data))
print("decoded:", data.decode("utf-8"))

Class example · 10 minutes

Predict the character count and UTF-8 byte count for A and 中. How can you check the original text is recovered?

Reveal example answer

Each string has one code point. A uses one UTF-8 byte; 中 uses three. Encode with text.encode("utf-8"), then decode those bytes with .decode("utf-8") and compare with the original.

Your work · 25 minutes

Encode and decode text with UTF-8 in Python. Compare len(text) with len(text.encode("utf-8")) for English and Chinese text. Distinguish characters, code points and bytes; one visible character can contain more than one code point. Save code and output.

Practice steps

Make a prediction/results table for A, 中 and A中. Encode and decode each. Explain why storing the byte count in a file-size calculation is different from counting visible letters. Save the code and labelled output.

Check and finish · 5 minutes

Explain character count versus byte count using your saved output.

Python practice · two short prompts

Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.

A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.

Prompt 1

Predict the code-point count and UTF-8 byte count for A中. Try your name as well.

No imports needed.

Hint and starter code

encode() converts text to bytes; len(text) counts code points, not always visible symbols.

# encode() converts text to bytes; len(text) counts code points, not always visible symbols.
text = "A中"
print(len(text))
print(len(text.encode("utf-8")))
Open this starter in Python playground →
Prompt 2

Recover the original text from data and print whether it matches.

No imports needed.

Hint and starter code

Use data.decode("utf-8"); use the same encoding in both directions.

# Use data.decode("utf-8"); use the same encoding in both directions.
text = "Hello 中"
data = text.encode("utf-8")
restored = ""  # Your turn: decode data
print(restored == text)
Open this starter in Python playground →
03 · 55 minutes

Bitmap images

Bitmap work started · colour depth introduced

Student workbook page 8 · Open lesson worksheet (Word)

Visual walkthrough

Pixels store colour choices

Trace the stages from left to right.

  1. 2 bits → 00, 01, 10, 11
  2. Four palette entries
  3. 4 × 4 pixels = 16 pixels
  4. 16 × 2 = 32 bits = 4 bytes
Discuss before continuing

Draw a 4 × 4 two-bit image. This raw pixel total excludes palette and file metadata.

Start · 5 minutes

What information must a computer know to turn pixel bits into a picture?

Explain and demonstrate · 10 minutes

A pixel is one picture element; a bitmap is a grid of pixels. Width × height gives pixel count. With b bits per pixel there are up to 2^b codes. Raw pixel bits = width × height × b; divide by eight for bytes. A palette maps codes to colours. Metadata describes the file, such as its dimensions; palette and metadata storage may add to the pixel data.

Demonstration

Draw a 4 × 4 pixel grid. Define a two-bit palette: 00 white, 01 blue, 10 black, 11 yellow. Encode one row left to right, then ask a partner to decode it using the palette. The bit pattern alone cannot tell the decoder the width, palette or row order. Doubling both dimensions multiplies the pixel count by four.

Class example · 10 minutes

A 8 × 8 bitmap uses one bit per pixel. How many colours and raw pixel bytes are possible?

Reveal example answer

2^1 = 2 colours. 8 × 8 × 1 = 64 bits = 8 bytes, excluding palette, metadata and compression.

Your work · 25 minutes

Choose image dimensions and a palette. Explain how bit depth limits the number of colours. Calculate raw pixel storage from width × height × bits per pixel. State units and whether palette or metadata storage is excluded.

Practice steps

Create and encode a 4 × 4 image with the two-bit palette. Calculate its raw pixel bits and bytes. Recalculate at 8 × 8, then with three bits per pixel. State the colour limit and units each time; separate palette/metadata from pixel storage.

Check and finish · 5 minutes

Show units at every stage. Explain how doubling both dimensions changes raw storage.

Python practice · two short prompts

Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.

A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.

Prompt 1

Calculate raw bitmap bytes, then double both dimensions. How does the size change?

No imports needed.

Hint and starter code

This counts pixel data only; depth is bits per pixel.

# This counts pixel data only; depth is bits per pixel.
width, height, depth = 8, 8, 2
bits = width * height * depth
print(bits, bits / 8)
Open this starter in Python playground →
Prompt 2

Print the colour limit for each bit depth. Add depth 3 and predict its limit.

No imports needed.

Hint and starter code

** means exponentiation: 2 ** depth is two to that power.

# ** means exponentiation: 2 ** depth is two to that power.
for depth in [1, 2, 4]:
    print(depth, 2 ** depth)
Open this starter in Python playground →
04 · 55 minutes

Sound and video

Gates introduced · signed numbers next

Student workbook page 9 · Open lesson worksheet (Word)

Visual walkthrough

Sampling records separate moments

Trace the stages from left to right.

  1. Time: 0 → 1 → 2 → 3
  2. Sound: one value per sample
  3. Video: one image per frame
  4. More moments → more raw data
Discuss before continuing

Sketch a wave and mark four sample points. What detail could be missed between samples?

Start · 5 minutes

What is the difference between samples per second and bits per sample?

Sound sampling studio

Amplitude is the wave’s height relative to its centre line. A sample is one measurement of that height. Frequency is how many wave cycles occur each second, measured in hertz (Hz): it affects pitch, how high or low this pure tone sounds. Sampling rate is how many samples are stored per second. Frequency and sampling rate are different quantities.

Bitmap bit depth counts possible colours. Audio bit depth counts possible amplitude levels: b bits give 2^b levels for each sample.

Think of bit depth as the marks on an amplitude ruler. At each sampling instant, measure the wave height and round to the nearest mark. A 2-bit ruler has 4 levels; 3 bits has 8; 4 bits has 16. More bits give finer measurements across the same amplitude range. The marks are redistributed evenly: adding one bit doubles the total levels. Sampling rate decides when you measure, not how fine the ruler is.

To quantize means to round a measured amplitude to the nearest available ruler mark; this rounding is called quantization. Pulse-code modulation (PCM) stores each regularly timed sample as a binary code for its quantized amplitude. A channel is one audio stream: mono has one; stereo has two.

This is a slowed visual demonstration, not sound playback. The graph shows 20 milliseconds (0.020 seconds). Normalized amplitude means the scale is expressed relative to a reference maximum, here from −1 to +1; it is not a decibel measurement of sound level.

Amplitude ruler: marks on the leftDashed curve: original waveVertical guides: sampling instantsDots and steps: quantized amplitudes
Original sound wave and stored samplesThe dashed curve is continuous. Regular vertical guides mark sampling times. Dots show amplitudes rounded to available levels. The selected sample is described in the table below. +10−105101520Time (ms)

Available amplitude levels: 16 (2 to the power of the bit depth). At high settings, only some level guides are drawn. The step line is a display aid, not a reconstructed sound wave.

Cycles in this 20 ms window: 4 · Time between samples (ms): 0.625

Selected sample — numeric alternative to the graph
Time (ms)Original amplitudeQuantized amplitudeStored code
000.0671000

The code is an illustrative unsigned level index: a non-negative label numbered from zero. Real PCM may use signed values, which can represent negative numbers. This model explains quantization, not a particular audio-file encoding. The plotted endpoint is a guide, not an extra sample added to the size calculation.

Calculate the uncompressed PCM sample data

Bits = samples per second × bits per sample per channel × seconds × channels

1600 × 4 × 4 × 1 = 25600 bits

Divide by 8 for bytes: 3200 bytes

3.2 kB · 0.0032 MB

3.125 KiB · 0.003052 MiB

Decimal: 1 kB = 1000 bytes; 1 MB = 1000000 bytes. Binary: 1 KiB = 1024 bytes; 1 MiB = 1048576 bytes. Displayed unit conversions are rounded.

This formula counts uncompressed PCM sample data only. It excludes a header (file information at the start), metadata (descriptive information), compression (a different representation intended to reduce storage), and padding (extra bits or bytes added for storage alignment). Changing pitch alone does not change this size when recording settings stay fixed.

Worked example and prediction

Four seconds of stereo audio at 8000 samples/s and 8 bits/sample/channel:

8000 × 8 × 4 × 2 = 512000 bits = 64000 bytes = 64 kB = 62.5 KiB

Predict: double the sampling rate, bit depth, duration or channels one at a time. Each doubles the PCM sample-data size. Increasing bit depth by one bit doubles the number of amplitude levels, but does not double file size: changing 8 bits to 9 bits multiplies size by 9/8.

Original classroom model

Codes need a playback clock

A sample code stores a quantized level: an amplitude rounded to one of the allowed levels. These unsigned 3-bit codes name levels 0–7; they are not volts without a scale. Keep sample rate separate from bit depth.

Use the buttons to step through. With scripts off, every frame remains readable.

Decode the level numbers

Decode the level numbers010 → 2101 → 5111 → 7000 → 0
  1. 010 → 2
  2. 101 → 5
  3. 111 → 7
  4. 000 → 0

Each code uses 3 bits and selects one of 8 allowed unsigned levels. Adding one bit doubles the number of levels, not the file size.

Read the diagram
  1. 010 → 2
  2. 101 → 5
  3. 111 → 7
  4. 000 → 0

Clock: 2 samples/s

Clock: 2 samples/s020.55171.502
  1. 0–0.5 s: level 2
  2. 0.5–1 s: level 5
  3. 1–1.5 s: level 7
  4. 1.5–2 s: level 0

Four equal playback slots: 2 seconds total. This sample-hold timeline is a teaching model; it does not claim every digital-to-analogue converter reconstructs a staircase.

Read the diagram
  1. 0–0.5 s: level 2
  2. 0.5–1 s: level 5
  3. 1–1.5 s: level 7
  4. 1.5–2 s: level 0

Same codes, faster clock

Same codes, faster clock020.2550.570.7501
  1. 0–0.25 s: 2
  2. 0.25–0.5 s: 5
  3. 0.5–0.75 s: 7
  4. 0.75–1 s: 0

At 4 samples/s the same four codes cover 1 second; first-to-last point span is 0.75 seconds. Playback speed changes because the clock changed, not because the stored codes changed.

Read the diagram
  1. 0–0.25 s: 2
  2. 0.25–0.5 s: 5
  3. 0.5–0.75 s: 7
  4. 0.75–1 s: 0

Four samples at 2 samples/s: why are playback duration and first-to-last point span different?

Discuss before revealing

Spacing is 1/fs = 0.5 s. Four playback periods last N/fs = 2 s. Points at 0, 0.5, 1 and 1.5 s span only (N−1)/fs = 1.5 s. The final period still lasts to 2 s. Codes alone do not specify fs.

Original classroom model

A video is a timed frame sequence

A frame is one image. Frame rate means frames per second. This original contact-sheet model excludes audio, compression and file headers. It is not an autoplaying video.

Use the buttons to step through. With scripts off, every frame remains readable.

Four successive frames

Four successive frames0123
  1. Frame 0: x = 0
  2. Frame 1: x = 1
  3. Frame 2: x = 2
  4. Frame 3: x = 3

Imagine a marker moving right by one grid position per frame. At 12 fps adjacent frame times are 1/12 s apart; at 24 fps they are 1/24 s apart.

Read the diagram
  1. Frame 0: x = 0
  2. Frame 1: x = 1
  3. Frame 2: x = 2
  4. Frame 3: x = 3

Raw video at 12 fps

Raw video at 12 fps320 × 240 pixels8 bits/pixel12 × 10 = 1209,216,000 bytes
  1. 320 × 240 pixels
  2. 8 bits/pixel
  3. 12 × 10 = 120
  4. 9,216,000 bytes

320 × 240 × 8 × 12 × 10 = 73,728,000 bits. Divide by 8 to get 9,216,000 bytes = 9.216 MB (decimal); divide bytes by 1,048,576 for MiB.

Read the diagram
  1. 320 × 240 pixels
  2. 8 bits/pixel
  3. 12 × 10 = 120
  4. 9,216,000 bytes

Double the frame rate

Double the frame rateSame pixelsSame bit depth24 × 10 = 24018,432,000 bytes
  1. Same pixels
  2. Same bit depth
  3. 24 × 10 = 240
  4. 18,432,000 bytes

The raw payload doubles to 18.432 MB (decimal). Real compressed files need not double because their codec may exploit similarity between frames.

Read the diagram
  1. Same pixels
  2. Same bit depth
  3. 24 × 10 = 240
  4. 18,432,000 bytes

What doubles when frame rate changes from 12 to 24 fps while all other settings stay fixed?

Discuss before revealing

For 10 seconds, frame count rises from 120 to 240 and raw storage doubles. Image size and colour depth per frame stay fixed. Audio bit depth instead changes the available amplitude levels per sample.

Explain and demonstrate · 10 minutes

A sample is one measured amplitude, or wave height. Sample rate counts measurements each second. Bit depth gives bits for each sample. A channel is one audio stream: mono has one and stereo has two. Raw audio bits = rate × depth × channels × seconds. A video frame is one still image; raw video bits = width × height × bits per pixel × frames per second × seconds.

Demonstration

Sketch a changing sound wave and mark equally spaced sample times. More samples capture more moments; greater bit depth permits more amplitude values. Stereo uses two channels. For video, calculate bits in one frame first, then multiply by the number of frames. Keep seconds, samples, pixels, bits and bytes labelled to avoid mixing units.

Class example · 10 minutes

Calculate two seconds of mono audio at 1000 samples per second and eight bits per sample.

Reveal example answer

1000 × 8 × 1 × 2 = 16000 bits = 2000 bytes. This is raw audio only; a file header adds data and compression changes file size.

Your work · 25 minutes

Compare sample rate and bit depth. Calculate raw audio bits from sample rate × bit depth × channels × seconds. Calculate raw video bits from frame size × bits per pixel × frames per second × seconds. State what is excluded, such as audio or metadata.

Practice steps

Calculate four seconds of stereo sound at 8000 samples/second and eight bits/sample. Then calculate two seconds of silent 10 × 10 video at eight bits/pixel and five frames/second. Compare what doubling one factor does and identify excluded data.

Check and finish · 5 minutes

Name every factor in one audio or video calculation. State what the estimate excludes.

Python practice · two short prompts

Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.

A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.

Prompt 1

Calculate the PCM sample-data size for this short recording. Change channels from 1 to 2 and explain the result.

No imports needed.

Hint and starter code

Use samples/second × bits/sample/channel × seconds × channels. No audio library is needed for arithmetic.

# Use samples/second × bits/sample/channel × seconds × channels.
rate, depth, seconds, channels = 4000, 8, 3, 1
bits = rate * depth * seconds * channels
print("bits:", bits)
print("bytes:", bits / 8)
Open this starter in Python playground →
Prompt 2

Print the marks on a 2-bit amplitude ruler. Change to 3 bits. Why does the total number of marks double?

No imports needed.

Hint and starter code

range(levels) counts from 0 to levels - 1. These normalized marks span -1 to +1; round() only shortens printed decimals.

# range(levels) counts from 0 to levels - 1.
depth = 2
levels = 2 ** depth
for code in range(levels):
    amplitude = -1 + 2 * code / (levels - 1)
    print(code, round(amplitude, 3))
Open this starter in Python playground →
05 · 55 minutes

Compression choices

Upcoming lesson resource · use after teaching

Student workbook page 10 · Open lesson worksheet (Word)

Visual walkthrough

Runs can shrink or expand

Trace the stages from left to right.

  1. AAAAAA → 6A
  2. ABABAB → 1A1B1A1B1A1B
  3. Rule: count then symbol
  4. Compare encoded size
Discuss before continuing

Assume one byte per count and symbol. Compare both sizes. Can you reconstruct the originals exactly?

Start · 5 minutes

When must decompressed data be exactly the same as the original?

Explain and demonstrate · 10 minutes

Compression changes how data is represented to try to use less storage. Lossless compression recovers exact original data; lossy compression discards some information. Run-length encoding (RLE) stores a count and symbol for each run, a consecutive repetition of one symbol; its size depends on the data and agreed format.

Demonstration

Write a run as one count digit followed by one symbol, with runs limited to nine for this exercise. Scan left to right, finish a run when the symbol changes, and start the next pair. Decode by repeating each symbol its count. Compare the stored characters using the same one-byte-per-character assumption; this model excludes a file header.

Class example · 10 minutes

Use one count character followed by one symbol. Encode BBBBAA and BABA. Compare stored character counts.

Reveal example answer

BBBBAA becomes 4B2A: four stored characters instead of six. BABA becomes 1B1A1B1A: eight instead of four. RLE can increase size when symbols change often.

Your work · 25 minutes

Compare lossless and lossy compression for text, pictures and sound. Define one run-length encoding format, including how counts are stored. Test repeated and alternating symbols. Explain why RLE can increase size.

Practice steps

Encode CCCCDD and CDCDCD, exchange with a partner and decode to check exact recovery. Count original and encoded characters. Decide whether lossless or lossy suits program code, a preview photograph and an archived measurement file, and justify each choice.

Check and finish · 5 minutes

Choose lossless or lossy for one use case and justify it. Explain when RLE helps or harms.

Original classroom model

Encode and decode a run

Run-length encoding (RLE) stores a repeat count and a symbol. This toy format uses one byte for each count and one byte for each letter, with runs limited to 1–9. No headers are counted.

Use the buttons to step through. With scripts off, every frame remains readable.

Repeated runs shrink

Repeated runs shrinkAAAA → 4ABBB → 3BCC → 2C9 bytes → 6
  1. AAAA → 4A
  2. BBB → 3B
  3. CC → 2C
  4. 9 bytes → 6

Concatenate the encoded runs: 4A3B2C. Decode by repeating each symbol its count; check all nine original letters.

Read the diagram
  1. AAAA → 4A
  2. BBB → 3B
  3. CC → 2C
  4. 9 bytes → 6

Alternating data expands

Alternating data expandsABABAB6 single runs1A1B1A1B1A1B6 bytes → 12
  1. ABABAB
  2. 6 single runs
  3. 1A1B1A1B1A1B
  4. 6 bytes → 12

Every run has length one, so each original letter needs two encoded bytes. Choose a compression method based on the data and need for exact recovery.

Read the diagram
  1. ABABAB
  2. 6 single runs
  3. 1A1B1A1B1A1B
  4. 6 bytes → 12

Is this lossless method always smaller? Decode both examples to justify your answer.

Discuss before revealing

No. AAAABBBCC becomes 4A3B2C: 9 bytes become 6. ABABAB becomes 1A1B1A1B1A1B: 6 bytes become 12. Both decode exactly; lossless means exact recovery, not guaranteed compression.

Python practice · two short prompts

Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.

A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.

Prompt 1

Decode these run-length pairs. Add one pair and predict the recovered text.

No imports needed.

Hint and starter code

A run is consecutive copies of the same symbol; each tuple stores (count, symbol).

# A run is consecutive copies of the same symbol; each tuple stores (count, symbol).
runs = [(3, "A"), (2, "B")]
text = ""
for count, symbol in runs:
    text += symbol * count
print(text)
Open this starter in Python playground →
Prompt 2

Compare the character counts of each original and its encoded text. Which grows?

No imports needed.

Hint and starter code

Assume one byte per character for this model; actual storage depends on the format.

# Assume one byte per character for this model; actual storage depends on the format.
for original, encoded in [("AAAABB", "4A2B"), ("ABAB", "1A1B1A1B")]:
    print(len(original), len(encoded))
Open this starter in Python playground →
Before you leave

Exit question

What extra information do you need to interpret a sequence of bits?