CSPIQ · AP Computer Science Principles · Lesson 3 of 25
CSPIQ · AP Computer Science Principles

Lesson 03: Binary: How Computers Represent Everything

Big Idea 2 (DAT) · Phase 2

Objectives

Warm-Up

Everything on your phone — this sentence, your last photo, the song playing, your high score — is stored as only two symbols: 0 and 1. Not because binary is elegant, but because hardware is reliable at exactly two states: voltage high or low, magnet north or south, light on or off. Two states you can distinguish cheaply and dependably, billions of times per second.

Quick challenge before we start: with 1 coin flip you can distinguish 2 outcomes. With 2 flips, 4 outcomes (HH, HT, TH, TT). With 10 flips? If you said 1,024 — or even "2 multiplied by itself 10 times" — you already understand the most-tested fact in Big Idea 2.


Core Concept

Bits, bytes, and the counting rule

A bit (binary digit) is the smallest unit of data: a single 0 or 1. A byte is 8 bits.

The counting rule — the most tested fact in this unit:

n bits can represent 2ⁿ different values.

Bits Distinct values Range if used for whole numbers starting at 0
1 2 0–1
2 4 0–3
3 8 0–7
4 16 0–15
8 256 0–255
n 2ⁿ 0 to 2ⁿ − 1

Two directions the exam asks this: - "How many values can 6 bits represent?" → 2⁶ = 64 - "A program must store 100 different ID numbers. What's the minimum number of bits?" → find the smallest n with 2ⁿ ≥ 100 → 2⁶ = 64 is too small, 2⁷ = 128 works → 7 bits - Adding one bit doubles the number of representable values. (Stated exactly this way in questions.)

Binary → decimal: place values

Binary is base 2: each position is worth a power of 2, growing right-to-left.

Binary:      1    0    1    1    0    1
Place:      32   16    8    4    2    1
Contribute: 32  +  0 +  8 +  4 +  0 +  1  =  45

So 101101₂ = 45. To convert binary → decimal: write the place values under the bits, add up the places holding a 1.

Decimal → binary: greedy subtraction

To convert 77 to binary: take the largest power of 2 that fits, subtract, repeat.

77 − 64 = 13        → 64s place: 1
13 − 8  = 5         → 8s place: 1   (32 and 16 don't fit: 0, 0)
5 − 4   = 1         → 4s place: 1
1 − 1   = 0         → 1s place: 1   (2s place: 0)

77 = 1001101₂

Verify by adding back: 64 + 8 + 4 + 1 = 77 ✓. Always verify — it takes five seconds and catches every slip.

(AP CSP uses unsigned whole numbers only — no negative binary, no two's complement, no binary fractions. If you've seen those elsewhere, park them; they're not on this exam.)

The same bits, many meanings: abstraction

Here's the deep idea. The byte 01000001 is: - the number 65, if read as a number - the letter "A", if read as text (character encodings like ASCII assign numbers to characters) - part of a color or a pixel brightness, if read as image data - a tiny slice of sound pressure, if read as audio

The bits don't "know" what they are. Meaning comes from how the program interprets them. That's abstraction: bits at the bottom; numbers built from bits; characters, colors, and sounds built from numbers; images and songs built from those. Each level ignores the details below it. When the CED says "data abstraction," at this level it means: anything digital is ultimately bits, and higher-level representations hide that detail.

Analog to digital: sampling

Real-world signals (sound waves, light, temperature) are analog — smooth and continuous. Computers store digital data — discrete values. The bridge is sampling: measure the analog signal at regular intervals, store each measurement as a number.

[DIAGRAM: A smooth sound wave (continuous curve). Vertical dashed lines at regular
time intervals mark sample points; at each, a dot on the curve shows the measured
value. Below, the list of measured values: 12, 19, 23, 21, 14, 6, 2, 5...
Caption: more frequent samples → closer approximation of the wave → more data.]

More samples per second and more bits per sample = a more faithful copy — and a bigger file. Digital data is an approximation of the analog original; finer sampling improves the approximation. (This trade-off returns in Lesson 4, compression.)

When numbers don't fit: overflow and roundoff

Computers store numbers in a fixed number of bits, so:

Exam framing: overflow = integer too large; roundoff = real (decimal) numbers stored as approximations. Different problems, different names.


Worked Examples

Example 1 (easy): Binary to decimal

Problem: What is 11010₂ in decimal?

Strategy: Place values right-to-left: 1, 2, 4, 8, 16. Add the places with 1s.

Solution:

Bits:   1   1   0   1   0
Place: 16   8   4   2   1
Sum:   16 + 8 + 0 + 2 + 0 = 26

11010₂ = 26

Interpretation: Write the places under the bits every single time. Doing it in your head is how careless errors happen under time pressure.

Example 2 (medium): Decimal to binary

Problem: Convert 52 to binary.

Strategy: Greedy subtraction with powers of 2: 64 is too big, start at 32.

Solution:

52 − 32 = 20   → 1 in 32s
20 − 16 = 4    → 1 in 16s
(8 doesn't fit into 4: 0)
4 − 4 = 0      → 1 in 4s
(2s: 0, 1s: 0)

52 = 110100₂

Verify: 32 + 16 + 4 = 52 ✓

Example 3 (medium): How many bits?

Problem: A school assigns each of its 1,000 students a unique binary ID. What is the minimum number of bits per ID?

Strategy: Smallest n with 2ⁿ ≥ 1000.

Solution: 2⁹ = 512 (too small). 2¹⁰ = 1024 ≥ 1000 ✓. 10 bits.

Interpretation: The wrong answers will include 9 (just under) and sometimes 1000/2 or other trash. Compute the powers — don't estimate.

Example 4 (AP-style): Interpreting the same bits differently

Problem: A program stores the value 01001000 01001001 (two bytes). One part of the program displays it as the text "HI"; another part reads the first byte as the number 72. Which statement best explains this?

(A) The program contains a logic error, since data can have only one meaning (B) The same sequence of bits can represent different types of data depending on how the program interprets it (C) The bits were corrupted between the two parts of the program (D) Text data cannot be represented in binary

Solution: (B). Bits have no inherent meaning; interpretation is applied by the program. 72 happens to be the character code for "H" — the same byte, two readings, both correct.

Interpretation: This is the abstraction question, and it appears on virtually every exam form in some costume. The answer always amounts to: meaning depends on interpretation.


Common Mistakes

  1. Confusing 2ⁿ with n². 8 bits give 2⁸ = 256 values, not 64. When rushed, students square. Slow down: doubling per bit, not squaring.
  2. Off-by-one on ranges. n bits represent 2ⁿ values, but the largest number (starting from 0) is 2ⁿ − 1. 8 bits: 256 values, max 255. Read whether the question asks "how many values" or "largest value."
  3. Misaligning place values. Writing 1, 2, 4, 8 left-to-right under a binary number. Place values grow right-to-left. Always label the 1s place first (rightmost), then move left.
  4. Bringing two's complement or binary fractions into AP CSP. Not in the CED. Binary here = unsigned whole numbers. If your answer requires negative binary, you've missed something simpler.
  5. Mixing up overflow and roundoff. Overflow = integer exceeded the maximum storable value. Roundoff = decimals stored as approximations. "0.1 + 0.2 ≠ 0.3 exactly" is roundoff, never overflow.

Practice Problems

Question 1
What is the decimal value of 1101₂?
Question 2
What is 19 in binary?
Question 3
How many different values can be represented with 5 bits?
Question 4
A robot's status must be one of: IDLE, MOVING, CHARGING, ERROR, PAUSED. What is the minimum number of bits needed to represent the status?
Question 5
Adding one bit to a binary representation:
Question 6
Using 8 unsigned bits, a program computes 200 + 100. The result is a(n):
Question 7
Which is the best explanation of why 0.1 + 0.2 may not equal exactly 0.3 in a program?
Question 8
A sound wave is converted to digital form by measuring its value 44,100 times per second. Increasing the measurement rate to 88,200 times per second will:
Question 9
The largest whole number representable with 6 unsigned bits is:
Question 10
(Select two answers.) Which of the following demonstrate that digital data representation involves abstraction?
Question 11
Which value, when converted to binary, requires the MOST bits?
Question 12
A weather station samples temperature once per hour and stores each reading. The stored data is best described as:

Create PT Connection

Binary itself won't appear in your Create PT code — but two ideas from this lesson will:

Habit: in your PT bug journal (started in Lesson 2), note any place your program deals with a number that could grow large or a decimal that could misbehave. One sentence each. Cheap insurance for WR 2(b).


Show answer key & explanations

(g) Answer Key

1. (B). Places: 8 4 2 1 under 1 1 0 1 → 8 + 4 + 0 + 1 = 13. (A) counts the bits' face value; (C) misplaces a bit (8+4+2=14); (D) doubles the true answer (misaligned places).

2. (A). 19 − 16 = 3; 3 − 2 = 1; 1 − 1 = 0 → 10011 (16 + 2 + 1). Verify: ✓. (B) is 25, (C) is 22, (D) is 21.

3. (D). 2⁵ = 32. (C) is 5² — the classic squaring mistake. (B) is 2×5, (A) is n itself.

4. (B). Five distinct states need 2ⁿ ≥ 5. 2² = 4 (too few), 2³ = 8 ✓ → 3 bits. (A) handles only 4 states; (D) confuses "5 states" with "5 bits."

5. (B). Each added bit doubles the count: 2ⁿ⁺¹ = 2 · 2ⁿ. Stated verbatim in the CED, asked verbatim on exams.

6. (A). Max for 8 unsigned bits = 255; 300 exceeds it → overflow. (B) confuses roundoff (decimals) with overflow (too large). (C) ignores the fixed width. (D): valid code, no grammar problem.

7. (B). Roundoff: finite bits approximate real numbers; small errors accumulate through arithmetic. (C) misapplies overflow to a small number.

8. (B). More samples → closer approximation of the analog wave AND more data to store. Fidelity and file size rise together — the trade-off behind next lesson's compression.

9. (C). 2⁶ − 1 = 63. (D) = 2⁶ counts the values (0 through 63) — the largest value is one less. This off-by-one is the single most common wrong answer in Big Idea 2.

10. (A) and (C). (A) is meaning-from-interpretation; (C) is layers hiding detail — both are abstraction in action. (B) is false (bit-widths vary); (D) is true-ish but says nothing about abstraction.

11. (C). 32 = 100000₂ (6 bits). 31 = 11111 (5 bits), 16 = 10000 (5 bits), 15 = 1111 (4 bits). Crossing a power of 2 adds a digit — just like 999 → 1000 in decimal.

12. (B). Sampling a continuous quantity at intervals produces a digital approximation. (A)/(C) claim perfection that sampling can't give; (D) misuses overflow.

Answer letter distribution check: B, A, D, B, B, A, B, B, C, A+C, C, B — singles: A×2, B×6, C×2, D×1 + multi (A,C). Still B-leaning; Lessons 4–5 keys will deliberately rebalance toward C/D.


Exam tip: Before the exam, hand-write the powers of 2 from 2⁰ to 2¹⁰ (1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024) on your scratch paper the moment the exam starts. Every binary question on the form becomes lookup + addition.

← All lessons
Lesson 4 ›
Score: 0/0 correct