Binary & Data Representation - Complete Interactive Lesson
Part 1: Core Concepts
💻 Binary & Data Representation
Part 1 of 7 — Binary Numbers, Conversions, and Digital Information
Why Binary?
Computers use binary (base-2) because electronic circuits have two states: ON (1) and OFF (0). ALL data — numbers, text, images, sound — is ultimately stored as sequences of 0s and 1s.
| Base | Name | Digits Used | Example |
|---|---|---|---|
| Base-2 | Binary | 0, 1 | 1010 |
| Base-10 | Decimal | 0-9 | 10 |
| Base-16 | Hexadecimal | 0-9, A-F | A |
Binary to Decimal Conversion
Each binary digit (bit) represents a power of 2:
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Example: 1101 in binary = ?
- 1 x 8 + 1 x 4 + 0 x 2 + 1 x 1 = 8 + 4 + 0 + 1 = 13
Example: 10110 in binary = ?
- 1 x 16 + 0 x 8 + 1 x 4 + 1 x 2 + 0 x 1 = 16 + 4 + 2 = 22
🔑 With n bits, you can represent 2^n different values (0 to 2^n - 1). 8 bits = 1 byte = 256 values.
Concept Check 🎯
Decimal to Binary Conversion
Repeatedly divide by 2 and track remainders (read bottom to top):
Convert 25 to binary:
- 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 upward: 11001
Overflow
When a value exceeds the number of bits available:
- 4 bits can store 0-15
- If you try to store 16 in 4 bits, it overflows back to 0
- This is like an odometer rolling over from 999 to 000
Data Units
| Unit | Size |
|---|---|
| Bit | Single 0 or 1 |
| Byte | 8 bits |
| Kilobyte (KB) | ~1,000 bytes |
| Megabyte (MB) | ~1,000 KB |
| Gigabyte (GB) | ~1,000 MB |
| Terabyte (TB) | ~1,000 GB |
Representing Other Data Types
- Text: Each character mapped to a number (ASCII: 7 bits, Unicode: up to 32 bits)
- Colors: RGB values (each 0-255 = 8 bits, total 24 bits per pixel)
- Sound: Sampled as numbers at regular intervals (sampling rate x bit depth)
Applied Recall ✍️
-
A single binary digit (0 or 1) is called a _______.
-
8 bits make up one _______.
-
With n bits, you can represent _______ to the power of n different values.
Binary Practice 🔍
AP Exam Strategy: Binary & Data
- Memorize powers of 2: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024
- Know both conversions: binary-to-decimal and decimal-to-binary
- With n bits: 2^n values, range 0 to 2^n - 1
- Overflow = value exceeds bit capacity. AP loves testing this concept
- Know how text (ASCII/Unicode), images (RGB pixels), and sound (samples) are stored in binary
- One additional bit DOUBLES the representable values — this is the key relationship
AP-Style Application 🎯
Part 2: Key Processes
🔢 Binary & Data Representation
Part 2 of 7 — Key Processes
How Computers Encode Everything in Binary
A computer represents every kind of data — numbers, text, images, sound — using only two digits: 0 and 1. The trick is agreement: a sender and receiver agree on a code, then the same bit pattern can mean a number, a letter, or a pixel color.
| Data type | Encoding scheme |
|---|---|
| Whole number | Place values 1, 2, 4, 8, 16, … (binary) |
| Text | A code mapping bit pattern → character (e.g., ASCII) |
| Color (pixel) | Three numbers: R, G, B — each 8 bits |
| Sound | Repeated samples of air-pressure amplitude |
Concept Check 🎯
Reading Binary Like a Pro
To convert binary to decimal, line up the bits with their place values from the right and add the place values where the bit is 1.
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 0 | 1 | 0 | 1 | 1 | 0 | 0 | 1 |
Sum the places where the bit is 1: 64 + 16 + 8 + 1 = 89.
Going The Other Way
To convert decimal → binary, repeatedly subtract the largest power of 2 that fits.
Convert 45:
- 32 fits → bit on; remainder 13.
- 16 doesn't fit; bit off.
- 8 fits → bit on; remainder 5.
- 4 fits → bit on; remainder 1.
- 2 doesn't fit; bit off.
- 1 fits → bit on; remainder 0.
Result: 00101101 (8 bits).
Why "n bits = values"
Each new bit doubles the patterns:
| Bits | Patterns |
|---|---|
| 1 | 2 |
| 2 | 4 |
| 4 | 16 |
| 8 | 256 |
| 16 | 65,536 |
| 32 | ~4.3 billion |
Memorize = 256 and = 65,536 — both appear constantly on the AP exam.
Applied Recall ✍️
-
A single binary digit (0 or 1) is called a _______.
-
The number of distinct values that can be represented with n bits is 2 to the _______ power.
-
The decimal number 5 in 4-bit binary is _______.
Targeted Practice 🔍
AP Exam Strategy: Binary Conversions
- Write the place-value row first; then plug in the bits.
- Memorize: =1, =2, =4, =8, =16, =32, =64, =128, =256, =1024.
- Decimal → binary: greedy subtraction by largest power of 2.
- For "how many bits do I need to represent N values?" use ⌈ N⌉.
AP-Style Application 🎯
Part 3: Patterns & Examples
🔢 Binary & Data Representation
Part 3 of 7 — Patterns & Examples
Patterns of Encoding
Computers use conventions to encode data types. Understanding the convention is half the work.
| Convention | Pattern |
|---|---|
| Unsigned integer | Plain place-value binary. |
| Signed integer | Reserves one bit for sign (e.g., two's complement). |
| Text (ASCII) | 7-bit code; "A" = 65, "a" = 97. |
| Text (Unicode) | Variable-length; supports every script. |
| RGB color | Three 8-bit channels: 0–255 red, green, blue. |
| Sample (audio) | Integer amplitude, repeated at sample rate. |
Concept Check 🎯
Worked Example: Decoding Text
Bytes: 72, 73, 33
- 72 = "H"
- 73 = "I"
- 33 = "!"
Output: HI! Since text is just numbers under an agreed mapping, any file is bytes — the difference is only how the program interprets them.
Worked Example: Building a Color
A pixel is three bytes. To make a strong purple:
| Channel | Value (0–255) |
|---|---|
| R | 128 |
| G | 0 |
| B | 128 |
In hex (the form you see in CSS): #800080. Each pair of hex digits = one byte (0x80 = 128).
Worked Example: Interpreting Audio
A WAV-style audio file stores samples of air-pressure amplitude:
- Sample rate: how many samples per second (e.g., 44,100 Hz for CD quality).
- Bit depth: bits per sample (e.g., 16 bits → 65,536 possible amplitudes).
- Channels: 1 (mono) or 2 (stereo).
A 60-second 16-bit stereo CD-quality clip = 60 × 44,100 × 2 (bytes/sample) × 2 (channels) = 10.6 MB.
Applied Recall ✍️
-
In ASCII the letters A and a have code points 65 and _______.
-
A 24-bit RGB pixel uses _______ bytes.
-
A digital audio file is built from repeated _______ of amplitude.
Targeted Practice 🔍
AP Exam Strategy: Encoding Conventions
- "How many bytes for a pixel?" → 3 bytes for 24-bit RGB; 4 bytes if there's an alpha channel.
- "How many bytes per second of audio?" → sampleRate × bytesPerSample × channels.
- ASCII letter math: subtract 65 to get the alphabetical position of an uppercase letter.
- Hex pairs = bytes. #FF0000 = 255-0-0 = bright red.
AP-Style Application 🎯
Part 4: Connections & Interactions
🔢 Binary & Data Representation
Part 4 of 7 — Connections & Interactions
Why Binary Connects To Everything Else
The "everything is bits" idea makes the rest of the course possible.
| Topic | Connection |
|---|---|
| Internet (BI 4) | All packets are bits, regardless of media (fiber, copper, radio). |
| Algorithms (BI 3) | Algorithms manipulate bit patterns — search, sort, compress. |
| Compression (BI 2) | Encode the same data in fewer bits. |
| Security (BI 4) | Encryption transforms bits into other bits unreadable without a key. |
| Impact (BI 5) | A digital file is trivial to copy and share — that changes society. |
Concept Check 🎯
Bits Are Format-Agnostic
A network router doesn't know whether the bits it forwards are an image, a song, or a chess move. It only knows source/destination headers and the payload bits. This is why a single Internet works for so many uses — the binary layer is universal.
Same Bytes, Different Meaning
A 4-byte sequence 01000001 01000010 01000011 01000100 could mean:
- Text (ASCII): "ABCD"
- Four small unsigned integers: 65, 66, 67, 68
- One 32-bit unsigned integer: 1,094,861,636
- Pixels: ¼ of a row of an image (each byte a grayscale level)
The interpretation lives in the program reading the file, not in the file itself. File extensions (".txt", ".png") and headers (magic bytes at the start of the file) tell programs how to interpret bytes.
Lossless Encoding Connections
Compression (next part) takes a bit pattern and produces a shorter one that decodes back to the same original. Encryption takes a bit pattern and produces a same-length one that only decodes with a key. Both rely on the universality of bits.
Applied Recall ✍️
-
On a network, all data — text, image, video — is transmitted as _______.
-
The same byte sequence can have different meanings depending on how the program _______ it.
-
A short magic-bytes signature at the start of a file tells programs the file _______.
Targeted Practice 🔍
AP Exam Strategy: Cross-Topic Binary Questions
- "How is X transmitted on the Internet?" → bits, regardless of X.
- "What does a byte sequence MEAN?" → the encoding scheme decides.
- Encryption and compression are both bit-pattern transformations.
- File extension ≠ guarantee: malicious bytes can be mislabeled. (Hence: never rely on extension for security.)
AP-Style Application 🎯
Part 5: Change Over Time
🔢 Binary & Data Representation
Part 5 of 7 — Change Over Time
How Storage And Encoding Have Scaled
Storage costs have collapsed and bit budgets have grown. The same data that filled a hard drive in 2000 fits in a phone's photo cache today.
| Era | Typical "lots of data" |
|---|---|
| 1980s | Megabytes. ASCII text dominated. |
| 1990s | Hundreds of MB. JPEG and MP3 made images/audio mainstream. |
| 2000s | Gigabytes. DVDs, MP3 collections, digital cameras. |
| 2010s | Terabytes. Streaming video became default. |
| 2020s | Petabytes (in datacenters). 4K, 8K video, AI training datasets. |
Concept Check 🎯
Lossless vs. Lossy
| Type | Guarantee | Examples | When to use |
|---|---|---|---|
| Lossless | Decoded data = original, bit for bit. | ZIP, PNG, FLAC | Documents, source code, medical imaging. |
| Lossy | Decoded data is approximately the original. | JPEG, MP3, MP4 | Photos, music, video where small artifacts are tolerable. |
Why Both Exist
- Lossless can't compress beyond a hard math limit (entropy). For a truly random file, no lossless compression helps.
- Lossy can compress much further by exploiting human perception (we don't notice many high frequencies in audio or subtle color shifts in images).
A 24-bit RGB photo: ~6 MB raw, ~600 KB as JPEG (10× smaller, almost identical to the eye).
Storage Scaling Has Changed Behavior
- Cheap storage made it normal to keep everything (logs, backups).
- AI training relies on datasets that would have been physically impossible to store in 2000.
- Cheap copies = quick spread = new questions about consent, copyright, and privacy.
Applied Recall ✍️
-
Compression that decodes back to a bit-for-bit identical original is called _______.
-
Compression that throws away some data to shrink the file is called _______.
-
JPEG and MP3 are examples of _______ compression.
Targeted Practice 🔍
AP Exam Strategy: Compression Choices
- "Original must be perfectly recoverable" → lossless.
- "File needs to be much smaller and small artifacts are okay" → lossy.
- "JPEG / MP3 / MP4" = lossy. "ZIP / PNG / FLAC" = lossless.
- Storage growth doesn't eliminate need for compression; it just changes the threshold of what's feasible.
AP-Style Application 🎯
Part 6: Problem-Solving Workshop
🔢 Binary & Data Representation
Part 6 of 7 — Problem-Solving Workshop
Binary & Data Workshop
Practice the calculations and design choices the AP exam asks under a time crunch.
Concept Check 🎯
Worked: Bit-Width Sizing
Question: "We have 5 different statuses to encode in each record. Minimum bits per record?"
- = 4 (too small).
- = 8 ≥ 5 ✓.
- Use 3 bits.
Question: "We have 1,000,000 user IDs. Minimum bits per ID?"
- = 524,288 (too small).
- = 1,048,576 ≥ 1M ✓.
- Use 20 bits.
Worked: Storage Estimation
Question: "Estimate the storage for 10,000 1024×1024 RGB photos, uncompressed."
- One photo: 1024 × 1024 × 3 ≈ 3 MB.
- 10,000 photos: ~30 GB.
With JPEG (~10×): ~3 GB.
Worked: Conversions Both Ways
| Decimal | Binary (8-bit) |
|---|---|
| 5 | 00000101 |
| 17 | 00010001 |
| 64 | 01000000 |
| 100 | 01100100 |
| 200 | 11001000 |
| 255 | 11111111 |
Applied Recall ✍️
-
For 100 students, the minimum bits required for unique IDs is _______.
-
A 32-bit number can store roughly _______ billion distinct values.
-
Binary 11111111 in decimal is _______.
Targeted Practice 🔍
AP Exam Strategy: Quick Calculations
- For "minimum bits for N values" → smallest n where ≥ N.
- For "storage of M items, each k bits" → M·k / 8 bytes.
- For audio: bytes/sec = sampleRate × bytesPerSample × channels.
- For images: bytes = width × height × bytesPerPixel.
- Don't worry about hex on the AP — it's rarely tested. Decimal/binary conversions and values are the core.
AP-Style Application 🎯
Part 7: AP Review
🔢 Binary & Data Representation
Part 7 of 7 — AP Review
AP Exam Recap — Binary & Data
Final cheat sheet of the binary and data-representation facts most tested on the AP exam.
Concept Check 🎯
Quick-Reference Table
| Memorize | Value |
|---|---|
| 1 byte | 8 bits |
| 256 | |
| 1,024 (≈ 1 KB) | |
| 65,536 | |
| ≈ 1 million (≈ 1 MB) | |
| ≈ 4.3 billion (≈ 4 GB) |
Common Misconceptions to Avoid
- "Binary uses 0 and 1 because computers think in numbers." (No — because hardware reliably represents two voltage levels.)
- "Lossy compression destroys the file." (No — it produces a smaller approximation; original can be saved separately.)
- "More bits is always better." (No — wasted bits = wasted storage / bandwidth. Use the minimum sufficient.)
- "Encryption shrinks data." (No — it transforms data; size stays roughly the same.)
Common Calculations
- Min bits for N values: ⌈ N⌉.
- Image bytes: W × H × bytesPerPixel.
- Audio bytes/sec: sampleRate × bytesPerSample × channels.
- Total storage of M items × k bytes each: M·k.
Applied Recall ✍️
-
8 bits = 1 _______.
-
The binary number 10000000 in decimal equals _______.
-
For 1,000,000 distinct IDs, the minimum number of bits required is _______.
Targeted Practice 🔍
Final Exam Tips
- "Smallest number of bits to represent N values" → ⌈ N⌉. Always.
- Distinguish lossless (perfectly reversible) from lossy (approximate).
- Encryption ≠ compression: encryption preserves size, compression shrinks it.
- Different file types are just different conventions for interpreting the SAME underlying bits.
- For storage estimates, multiply: items × bytes-per-item.
AP-Style Application 🎯