AP Computer Science Principles · Data
Binary numbers
1Bits and bytes
Computers store all data (numbers, text, images, sound) as bits: binary digits, 0 or 1. A group of 8 bits is a byte.
With $n$ bits you can represent $2^n$ different values:
- 1 bit: 2 values; 4 bits: 16; 8 bits: 256; 16 bits: 65,536.
Each extra bit doubles the number of values.
Check yourself
How many different values can 6 bits represent?
Show the answer
64
2⁶.
2Binary and decimal
Decimal (base 10) uses place values 1, 10, 100, … Binary (base 2) uses place values 1, 2, 4, 8, 16, 32, 64, 128, …
Binary → decimal: add the place values where there is a 1.
$10110_2$.
- Place values from the right: 1, 2, 4, 8, 16.
- Bits: 1 0 1 1 0 → $16 + 4 + 2 = 22$.
Decimal → binary: subtract the largest place values that fit.
45.
- 32 fits → 13 left; 16 does not; 8 fits → 5 left; 4 fits → 1 left; 2 does not; 1 fits.
- $45 = 101101_2$.
Binary place values double from the right; add the places that hold a 1
Check yourself
What is 1101 in binary as a decimal number?
Show the answer
13
8 + 4 + 1.
3Limits: overflow and round-off
Computers store numbers in a fixed number of bits, which causes limits:
- Overflow error: a result is too large to fit. With 4 bits the largest value is 15; adding 1 to 15 cannot be stored.
- Round-off error: real numbers like $\frac{1}{3}$ or 0.1 cannot be stored exactly in binary, so values are approximated and small errors accumulate.
Some languages (including the AP pseudocode) assume integers are unlimited, but real computers have limits.
Check yourself
Using 4 bits, what happens when 1 is added to 1111?
Show the answer
Overflow: the result 16 cannot be stored
16 needs 5 bits.
4Abstraction: bits can mean anything
The same bits can represent different things depending on how a program interprets them: a number, a character, a colour, part of a sound. This is an example of abstraction: the meaning comes from the context.
Examples:
- Text: each character has a numeric code (e.g. "A" is 65).
- Colour: three bytes for red, green and blue intensity (0–255 each), giving $256^3 \approx 16.7$ million colours.
- Sound: the height of a wave measured many times per second (sampling).
Analog data (continuous, like sound waves) is approximated by sampling, measuring at regular intervals. More samples and more bits per sample give higher quality but larger files.
Check yourself
Why does sampling only approximate an analog signal?
Show the answer
It records values at intervals, missing what happens in between
Discrete samples.
5Going deeper: conversions, how many values bits can represent, and the limits of representation
Why binary? Computer hardware stores and moves data using components with two states (on or off, high or low voltage). A bit is one binary digit, 0 or 1. A byte is 8 bits.
Place values in binary are powers of 2, from the right: 1, 2, 4, 8, 16, 32, 64, 128 …
Binary to decimal: add the place values where there is a 1.
110101.
- Place values: 32, 16, 8, 4, 2, 1.
- Ones at 32, 16, 4 and 1.
- 32 + 16 + 4 + 1 = 53.
Decimal to binary: subtract the largest power of 2 that fits, and repeat.
45.
- 32 fits: 45 − 32 = 13. (1)
- 16 does not fit. (0)
- 8 fits: 13 − 8 = 5. (1)
- 4 fits: 5 − 4 = 1. (1)
- 2 does not fit. (0)
- 1 fits. (1)
- 101101. Check: 32 + 8 + 4 + 1 = 45.
How many values? With n bits there are 2ⁿ different patterns.
- 1 bit: 2. 2 bits: 4. 3 bits: 8. 4 bits: 16. 8 bits: 256. 10 bits: 1024.
- Each extra bit doubles the number of patterns.
- Counting from zero, the largest value is 2ⁿ − 1: with 8 bits, 0 to 255.
how many bits are needed to give each of 200 students a unique ID?
- 7 bits give 128 patterns: too few.
- 8 bits give 256: enough.
- 8 bits.
a system uses 6 bits for product codes and is changed to 7 bits. How many more products can it identify?
- 6 bits: 64. 7 bits: 128.
- Twice as many (64 more).
Comparing binary numbers: with the same number of bits, compare from the left; the first position where they differ decides.
Everything is bits. The same sequence of bits can stand for a number, a character, a color, a sound sample or an instruction; how it is interpreted depends on the context. This is abstraction: we work with numbers, letters and images without thinking about the bits underneath.
- Text: each character has a numeric code. In ASCII, capital A is 65, which is 1000001.
- Color: in the RGB scheme, red, green and blue each get 8 bits (0 to 255), giving 24 bits and about 16.7 million colors.
- Images: a grid of pixels, each with a color value.
- Sound: thousands of measurements (samples) per second, each stored as a number.
Analog and digital: analog data vary continuously (the pressure of a sound wave, the brightness of a scene). Digital data are discrete. To store analog data, a computer takes samples at regular intervals and records each as a number. More samples and more bits per sample give a closer approximation, and a larger file; some detail between samples is always lost.
Limits of representation
Overflow: with a fixed number of bits, some integers are too big.
- With 4 bits the largest value is 1111 = 15. Adding 1 needs a fifth bit (10000), which is not available: an overflow error. The stored result is wrong (often wrapping to 0).
- Many languages use a fixed number of bits for integers; some allow integers to grow, limited only by memory.
Round-off: real numbers are stored as approximations, because a fixed number of bits cannot represent every fraction exactly. One-third cannot be written exactly in decimal; likewise one-tenth cannot be written exactly in binary. So 0.1 + 0.2 may not equal exactly 0.3. Tiny errors can build up over many calculations.
Worked example (adding in binary): 0110 + 0011.
- 6 + 3.
- From the right: 0 + 1 = 1; 1 + 1 = 10, write 0 carry 1; 1 + 0 + 1 = 10, write 0 carry 1; 0 + 0 + 1 = 1.
- 1001 = 9.
Common mistakes
- Saying n bits can represent n values, or 2n.
- Giving the largest n-bit value as 2ⁿ (it is 2ⁿ − 1).
- Reading place values from the left.
- Confusing overflow with round-off.
- Thinking the computer "knows" whether bits are a number or a letter; the program decides.
Check yourself
How many different values can be represented with 5 bits?
Show the answer
32
Two choices for each of five bits: 2⁵.
6For top marks
- Use $2^n$ to find how many values $n$ bits can represent, and work backwards: to represent 200 values you need 8 bits ($2^7 = 128 < 200 \le 256$).
- Convert both ways quickly with place values.
- Distinguish overflow (too big) from round-off (not exact).
- Compare binary numbers by converting, or by comparing from the leftmost bit.
"To give 1,000 students unique IDs in binary, 10 bits are needed, since $2^9 = 512 < 1000 \le 1024 = 2^{10}$."
Check yourself
What is the minimum number of bits needed for 300 different values?
Show the answer
9
2⁸ = 256 < 300 ≤ 512.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera