Skip to content
🎯⭐ INTERACTIVE LESSON

Binary & Data Representation

Learn step-by-step with interactive practice!

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.

BaseNameDigits UsedExample
Base-2Binary0, 11010
Base-10Decimal0-910
Base-16Hexadecimal0-9, A-FA

Binary to Decimal Conversion

Each binary digit (bit) represents a power of 2:

Position76543210
Value1286432168421

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

UnitSize
BitSingle 0 or 1
Byte8 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 ✍️

  1. A single binary digit (0 or 1) is called a _______.

  2. 8 bits make up one _______.

  3. 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 typeEncoding scheme
Whole numberPlace values 1, 2, 4, 8, 16, … (binary)
TextA code mapping bit pattern → character (e.g., ASCII)
Color (pixel)Three numbers: R, G, B — each 8 bits
SoundRepeated 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 value1286432168421
Bit01011001

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 = 2n2^{n} values"

Each new bit doubles the patterns:

BitsPatterns
12
24
416
8256
1665,536
32~4.3 billion

Memorize 282^{8} = 256 and 2162^{16} = 65,536 — both appear constantly on the AP exam.

Applied Recall ✍️

  1. A single binary digit (0 or 1) is called a _______.

  2. The number of distinct values that can be represented with n bits is 2 to the _______ power.

  3. 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: 202^{0}=1, 212^{1}=2, 222^{2}=4, 232^{3}=8, 242^{4}=16, 252^{5}=32, 262^{6}=64, 272^{7}=128, 282^{8}=256, 2102^{10}=1024.
  • Decimal → binary: greedy subtraction by largest power of 2.
  • For "how many bits do I need to represent N values?" use ⌈log⁡2\log_{2} 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.

ConventionPattern
Unsigned integerPlain place-value binary.
Signed integerReserves 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 colorThree 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:

ChannelValue (0–255)
R128
G0
B128

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 ✍️

  1. In ASCII the letters A and a have code points 65 and _______.

  2. A 24-bit RGB pixel uses _______ bytes.

  3. 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.

TopicConnection
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 ✍️

  1. On a network, all data — text, image, video — is transmitted as _______.

  2. The same byte sequence can have different meanings depending on how the program _______ it.

  3. 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.

EraTypical "lots of data"
1980sMegabytes. ASCII text dominated.
1990sHundreds of MB. JPEG and MP3 made images/audio mainstream.
2000sGigabytes. DVDs, MP3 collections, digital cameras.
2010sTerabytes. Streaming video became default.
2020sPetabytes (in datacenters). 4K, 8K video, AI training datasets.

Concept Check 🎯

Lossless vs. Lossy

TypeGuaranteeExamplesWhen to use
LosslessDecoded data = original, bit for bit.ZIP, PNG, FLACDocuments, source code, medical imaging.
LossyDecoded data is approximately the original.JPEG, MP3, MP4Photos, 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 ✍️

  1. Compression that decodes back to a bit-for-bit identical original is called _______.

  2. Compression that throws away some data to shrink the file is called _______.

  3. 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?"

  • 222^{2} = 4 (too small).
  • 232^{3} = 8 ≥ 5 ✓.
  • Use 3 bits.

Question: "We have 1,000,000 user IDs. Minimum bits per ID?"

  • 2192^{19} = 524,288 (too small).
  • 2202^{20} = 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

DecimalBinary (8-bit)
500000101
1700010001
6401000000
10001100100
20011001000
25511111111

Applied Recall ✍️

  1. For 100 students, the minimum bits required for unique IDs is _______.

  2. A 32-bit number can store roughly _______ billion distinct values.

  3. Binary 11111111 in decimal is _______.

Targeted Practice 🔍

AP Exam Strategy: Quick Calculations

  • For "minimum bits for N values" → smallest n where 2n2^{n} ≥ 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 2n2^{n} 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

MemorizeValue
1 byte8 bits
282^{8}256
2102^{10}1,024 (≈ 1 KB)
2162^{16}65,536
2202^{20}≈ 1 million (≈ 1 MB)
2322^{32}≈ 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: ⌈log⁡2\log_{2} N⌉.
  • Image bytes: W × H × bytesPerPixel.
  • Audio bytes/sec: sampleRate × bytesPerSample × channels.
  • Total storage of M items × k bytes each: M·k.

Applied Recall ✍️

  1. 8 bits = 1 _______.

  2. The binary number 10000000 in decimal equals _______.

  3. 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" → ⌈log⁡2\log_{2} 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 🎯