2.1 Binary Numbers
All data on a computer is stored as sequences of bits. You need to convert between binary and decimal fluently and understand how the number of bits limits what can be represented.
What you need to know
- A bit is a single binary digit: 0 or 1. A byte is 8 bits.
- Binary is base 2: each place value is a power of 2 (from the right: 1, 2, 4, 8, 16, 32, 64, 128 …). Decimal is base 10, with place values that are powers of 10.
- Binary → decimal: add the place values where there's a 1.
1101= 8 + 4 + 0 + 1 = 13. - Decimal → binary: repeatedly subtract the largest power of 2 that fits. 22 → 16 (leaves 6) → 4 (leaves 2) → 2 (leaves 0), so 22 =
10110. - n bits can represent 2n distinct values. 8 bits → 256 values (0–255). Adding one bit doubles the number of possible values.
- Numbers, text, images, and sound are all stored as binary once encoded. Text uses a scheme that maps each character to a number (ASCII, Unicode); an image maps each pixel to numbers for its color; audio is sampled at regular intervals and each sample stored as a number.
- A fixed number of bits for a value means a maximum representable size — the source of overflow errors. Some languages use a fixed size; others (and AP pseudocode) treat integers as arbitrary-size, so overflow is a real-world issue rather than a pseudocode one.
- Real numbers are stored with limited precision, so calculations can produce small round-off errors.
Worked example
Convert 10011 to decimal. Place values from the right: 1, 2, 4, 8, 16. Ones are in the 16, 2, and 1 places → 16 + 2 + 1 = 19.
Convert 45 to binary. 32 fits (13 left) → 8 fits (5 left) → 4 fits (1 left) → 1 fits (0 left). Bits set: 32, 8, 4, 1 → 101101. Check: 32 + 8 + 4 + 1 = 45.
How many colors can 3 bits represent? 23 = 8. With 4 bits, 16. Each added bit doubles the count.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- The CED asks you to understand place value in both bases, not just memorize conversions. In decimal, 352 = 3×100 + 5×10 + 2×1. In binary, 101 = 1×4 + 0×2 + 1×1. Same idea, different base.
- Analog vs. digital: analog data (sound waves, temperature) is continuous. Digital data is discrete. Converting analog to digital means sampling — measuring at regular intervals — and each sample is stored as a number. More samples per second and more bits per sample = higher fidelity and larger file.
- Bit width limits matter in real languages. An 8-bit unsigned integer holds 0–255; add 1 to 255 and it wraps to 0 (overflow). A 32-bit signed integer maxes at about 2.1 billion. AP pseudocode ignores this, but the concept is tested.
- Round-off error comes from real numbers: 1/3 can't be stored exactly in any finite number of bits, in any base. So 0.1 + 0.2 might not exactly equal 0.3 in a computer. This is why financial software often works in cents (integers) rather than dollars (decimals).
- Converting binary to decimal quickly: write the place values above the digits from the right (1, 2, 4, 8, 16, 32, 64, 128). Converting decimal to binary: subtract the largest power of 2 that fits, repeat. Or divide by 2 repeatedly and read the remainders bottom-up.
- The number of bits needed to represent N distinct values is the smallest n with 2n ≥ N. 100 values need 7 bits (27 = 128). 1,000 values need 10 bits.
Mistakes that cost points
- Off-by-one on place values. The rightmost bit is worth 1, not 2. Students who start at 2 get every conversion wrong by a factor.
- Answering with the number of bits instead of the number of values. "How many values can 5 bits represent?" is 32, not 5. "How many bits to represent 32 values?" is 5.
- Thinking adding a bit adds a fixed number of values. It doubles them. 3 bits → 8, 4 bits → 16, 5 bits → 32.
- Confusing sampling rate with bit depth. Rate = how often you measure; depth = how many bits each measurement gets. Both affect quality and size, in different ways.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Show answer
Answer: C. Place values 32, 16, 8, 4, 2, 1 with bits 1,0,1,1,1,0 → 32 + 8 + 4 + 2 = 46.
Show answer
Answer: B. 4 bits gives 2⁴ = 16 values, not enough. 5 bits gives 2⁵ = 32 values, which covers 20. The answer is the smallest n with 2ⁿ ≥ 20.
Show answer
Answer: C. All data types are ultimately binary. Adding a bit doubles (not halves) values, and a byte represents 256 values, not 8.
Key vocabulary
- Bit
- a single binary digit, 0 or 1
- Byte
- 8 bits
- Binary (base 2)
- a number system with two digits, where each place is a power of 2
- Decimal (base 10)
- the everyday number system, where each place is a power of 10
- Overflow error
- a value exceeds what its fixed number of bits can hold
- Sampling
- measuring an analog signal, like sound, at regular intervals to store it digitally