Skip to content
๐ŸŽฏโญ INTERACTIVE LESSON

Binary & Data Representation

Learn step-by-step with interactive practice!

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.

BaseNameDigits UsedExample
Base-2Binary0, 11010
Base-10Decimal0-910

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:

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
  • 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

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

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 โœ๏ธ

  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 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 typeEncoding scheme
Whole numberPlace values 1, 2, 4, 8, 16, โ€ฆ (binary)
TextAn agreed-upon code mapping each bit pattern to a character
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?" find the smallest n with 2n2^{n} โ‰ฅ 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.

DataPattern
Whole numberPlace-value binary: 1, 2, 4, 8, 16, โ€ฆ
TextEach character is an agreed-upon bit pattern.
RGB colorThree 8-bit channels: 0โ€“255 red, green, blue.
Sound sampleA 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:

ChannelValue (0โ€“255)
R128
G0
B128

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 โœ๏ธ

  1. Data whose values change smoothly over time, rather than in discrete steps, are called _______ data.

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

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 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 โœ๏ธ

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

BitsValuesLargest whole number
41615
8256255
1665,53665,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.

BinaryDecimal
10019
101111
111014

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 โœ๏ธ

  1. When a value needs more bits than a fixed-size integer has, the result is an _______ error.

  2. The largest whole number that can be stored in 6 bits is _______.

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

  • 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 lossy compression that shrinks each photo about 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.
  • Hexadecimal is not on the AP exam. 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.)
  • "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 2n2^{n} โ‰ฅ 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" โ†’ the smallest n with 2n2^{n} โ‰ฅ 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 ๐ŸŽฏ