Binary & Data Representation - Complete Interactive Lesson
Part 1: Binary Numbers, Conversions, and Digital Information
๐ป 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 |
The AP exam uses only binary and decimal; hexadecimal is not tested.
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
- Trying to store 16 in 4 bits causes an overflow error: the value needs more bits than are available, so the program either stops with an error or produces a wrong result
- This happens in languages that store integers in a fixed number of bits. The language on the AP exam reference sheet does not have this limit: its integers are limited only by the computer's memory
Round-off Error
Real numbers (numbers with a decimal part) are also stored in a fixed number of bits, so many of them can only be stored as approximations. Calculations with them can produce small round-off errors (for example, a result that should be 0.3 printing as 0.30000000000000004).
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
Bits are grouped to represent abstractions such as numbers, characters, and colors:
- Text: Each character is stored as a bit pattern under an agreed-upon code
- Colors: RGB values (each 0-255 = 8 bits, total 24 bits per pixel)
- Sound: Analog sound is sampled: its value is measured at regular intervals and each sample is stored in bits
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 a fixed number of bits; round-off = a real number can only be stored approximately
- Know that text, images (RGB pixels), and sound (samples) are all stored as bits, and that the same bits can mean different things in different contexts
- 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 | An agreed-upon code mapping each bit pattern to a character |
| 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?" find the smallest n with โฅ N.
- To compare two binary numbers of the same length, read from the left: the first position where they differ decides which is larger.
AP-Style Application ๐ฏ
Part 3: Patterns & Examples
๐ข Binary & Data Representation
Part 3 of 7 โ Patterns & Examples
Patterns of Encoding
Bits are grouped to represent abstractions such as numbers, characters, and colors. A program and its users agree on a convention, and understanding the convention is half the work.
| Data | Pattern |
|---|---|
| Whole number | Place-value binary: 1, 2, 4, 8, 16, โฆ |
| Text | Each character is an agreed-upon bit pattern. |
| RGB color | Three 8-bit channels: 0โ255 red, green, blue. |
| Sound sample | A measured value, repeated at regular intervals. |
The same sequence of bits can represent different types of data in different contexts. The meaning comes from the program that reads the bits.
Concept Check ๐ฏ
Worked Example: Same Bits, Different Meaning
The byte 01001000 could be:
- the whole number 72 (64 + 8),
- one character of a text message, under the code the program uses,
- the brightness of one color channel in a pixel.
Since text, numbers, and colors are all bit patterns, any file is just bits; 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 |
Each channel is one byte, so the pixel takes 3 bytes (24 bits). Since 128 = 10000000 in binary, the pixel's bits are 10000000 00000000 10000000.
Worked Example: Analog to Digital
Analog data change smoothly over time rather than in discrete steps: the pitch and volume of music, the colors of a painting, the position of a sprinter. A computer approximates analog data with sampling: it measures the signal at regular intervals (samples) and stores each sample in bits. Using digital data to approximate analog data is an example of abstraction.
An audio file stores samples of the sound wave:
- 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. Taking samples more often gives a closer approximation of the original sound, but it needs more bits.
Applied Recall โ๏ธ
-
Data whose values change smoothly over time, rather than in discrete steps, are called _______ data.
-
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 (one byte per channel).
- "How many bytes per second of audio?" โ sampleRate ร bytesPerSample ร channels.
- "What does this bit pattern mean?" โ it depends on the context; the same bits can be a number, a character, or a color.
- Sampling analog data is an abstraction: more frequent samples = closer approximation = more bits.
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 5) | 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: the letters "ABCD", under a common character code
- Four small unsigned integers: 65, 66, 67, 68
- One large whole number: all 32 bits read together as a single binary number
- Pixels: four grayscale brightness levels, one byte each
The interpretation lives in the program reading the file, not in the file itself. This is the CED idea that the same sequence of bits may represent different types of data in different contexts.
Lossless Encoding Connections
Lossless compression takes a bit pattern and produces a shorter one that decodes back to the same original. Encryption takes a bit pattern and produces one that can only be decoded 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.
-
Using digital data to approximate real-world analog data is an example of _______.
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.
AP-Style Application ๐ฏ
Part 5: Limits of Fixed Bits: Overflow and Round-off
๐ข Binary & Data Representation
Part 5 of 7 โ Limits of Fixed Bits: Overflow and Round-off
When the Bits Run Out
In many programming languages, an integer is stored in a fixed number of bits. That fixes the largest value it can hold:
| Bits | Values | Largest whole number |
|---|---|---|
| 4 | 16 | 15 |
| 8 | 256 | 255 |
| 16 | 65,536 | 65,535 |
If a calculation produces a value that needs more bits than are available, the result is an overflow error: the program stops with an error or stores a wrong value. For example, an 8-bit integer holding 255 cannot hold 255 + 1.
๐ The language on the AP exam reference sheet is different: its integers are limited only by the size of the computer's memory, so exam programs do not overflow.
Concept Check ๐ฏ
Round-off Error
Real numbers (numbers with a fractional part, like 0.1 or 3.75) are also stored in a fixed number of bits. Many of them, such as 1/3 or 0.1, cannot be written exactly in binary, so the computer stores an approximation. Arithmetic on these approximations can produce round-off errors:
- 0.1 + 0.2 may be stored as 0.30000000000000004.
- A test such as IF(total = 0.3) can then be false even though the math says it should be true.
You do not need to know the exact range of real numbers a computer can store; that is outside the AP exam. You do need to know why the errors happen: a fixed number of bits.
Comparing and Ordering Binary Numbers
To compare two binary numbers, give them the same length by adding leading zeros, then read from the left. The first position where they differ decides: the number with the 1 there is larger.
| Binary | Decimal |
|---|---|
| 1001 | 9 |
| 1011 | 11 |
| 1110 | 14 |
1001 vs 1011: they first differ in the 2s place, and 1011 has the 1, so 1011 is larger. Ordered least to greatest: 1001, 1011, 1110.
Applied Recall โ๏ธ
-
When a value needs more bits than a fixed-size integer has, the result is an _______ error.
-
The largest whole number that can be stored in 6 bits is _______.
-
Binary 1110 in decimal is _______.
Targeted Practice ๐
AP Exam Strategy: Consequences of Using Bits
- Overflow = an integer result too large for a fixed number of bits.
- Round-off = a real number that can only be stored approximately.
- The exam's own pseudocode language has no integer overflow; questions about overflow describe some other language with fixed-size integers.
- To compare binary numbers, line them up on the right and read from the left; the first difference decides.
- When converting to check an answer, use place values: 1, 2, 4, 8, 16, 32, 64, 128.
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 lossy compression that shrinks each photo about 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.
- Hexadecimal is not on the AP exam. 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.)
- "Overflow can't happen in AP pseudocode, so it doesn't matter." (No โ the exam asks about overflow in languages that store integers in a fixed number of bits.)
Common Calculations
- Min bits for N values: the smallest n with โฅ 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" โ the smallest n with โฅ N. Always.
- Distinguish lossless (perfectly reversible) from lossy (approximate).
- Overflow (integer too big for its bits) โ round-off (real number stored approximately).
- Different file types are just different conventions for interpreting the SAME underlying bits.
- For storage estimates, multiply: items ร bytes-per-item.
AP-Style Application ๐ฏ