AP® Computer Science Principles review sheet from Aim for Five (aimforfive.com/csp/units/2/2-1)
Unit 2 · Topic 2.1
2.1 Binary Numbers
Everything a computer stores, from numbers to songs, is ultimately made of bits: 0s and 1s. This topic covers how bits represent data, how to convert between binary and decimal by hand, and what goes wrong when a computer has only a fixed number of bits to work with.
Key terms
- bit
- byte
- binary (base 2)
- overflow error
- round-off error
- analog data and sampling
Bits, bytes and abstraction
A bit (short for binary digit) is the smallest unit of data: a single 0 or 1. A byte is 8 bits. Computing devices are digital, which means that at the lowest level every value they store is a pattern of bits.
Bits are grouped to stand for bigger ideas: numbers, letters, colors, sounds. That's an abstraction, which means hiding the details you don't need so you can focus on the main idea. You think "the letter A" or "the color red," not "01000001."
The same bits can mean different things in different contexts. The byte 01000001 is the number 65 when read as a whole number, and it's the letter A in the common ASCII text code. A color is often stored as three bytes, one each for red, green and blue, each from 0 to 255. The program decides how to read the bits.
Data values can be stored in variables, in lists, or as fixed constants, and they can be passed into and returned from procedures.
Binary and decimal place values
Decimal (base 10) uses the digits 0 to 9. Binary (base 2) uses only 0 and 1. In both, a digit's position sets its value: each place is the base raised to a power, starting at power 0 on the far right.
In decimal, the places are 1, 10, 100, 1000 and so on. In binary, they're 1, 2, 4, 8, 16, 32, 64, 128, doubling each time you move left.
Binary to decimal: multiply each bit by its place value and add. For 1101: 8 + 4 + 0 + 1 = 13.
Decimal to binary: find the largest place value that fits, put a 1 there, subtract, and repeat with what's left; every place you skip gets a 0.
| Place (power of 2) | 2⁷ | 2⁶ | 2⁵ | 2⁴ | 2³ | 2² | 2¹ | 2⁰ |
|---|---|---|---|---|---|---|---|---|
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
How many values can n bits hold?
With n bits you can make 2ⁿ different patterns. If they stand for whole numbers starting at 0, the range is 0 to 2ⁿ − 1. So 4 bits hold 0 to 15, and 8 bits (one byte) hold 0 to 255.
Each extra bit doubles the number of values, it doesn't just add one. To give 100 students each a different ID, you need 7 bits, because 2⁶ = 64 is too few and 2⁷ = 128 is enough.
To compare binary numbers with the same number of digits, read from the left: the first place where they differ decides which is bigger. 1011 is larger than 1001 because they first differ in the 2s place.
When bits run out: overflow and round-off
Many programming languages store whole numbers in a fixed number of bits. That limits the range, and a result outside it causes an overflow error. If a program stores whole numbers in 4 bits, 15 + 1 doesn't fit, because 16 needs 5 bits.
Some languages avoid this by letting whole numbers grow as large as the computer's memory allows. The exam's pseudocode works this way, so overflow only comes up in questions about a fixed number of bits.
Real numbers (numbers with decimals) are also stored in a fixed number of bits, so many can only be approximated. That causes round-off errors. In many languages 0.1 + 0.2 comes out as 0.30000000000000004, not exactly 0.3. You won't be asked the exact ranges for real numbers.
Analog data and sampling
Analog data changes smoothly over time instead of in separate steps: the pitch of a voice, the brightness across a sunset, the position of a runner. Computers can't store infinitely smooth values, so they approximate them.
Sampling means measuring the analog signal at regular intervals. Each measurement, called a sample, is stored as bits. Taking more samples per second, or using more bits per sample, makes the digital copy closer to the original but uses more storage. Representing analog data digitally is another example of abstraction.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Binary to decimal
What decimal number does the binary number 101101 represent?
Show the solutionHide the solution
- Step 1: Write the place values under the six bits, from the right: 32, 16, 8, 4, 2, 1.
- Step 2: Bits: 1 0 1 1 0 1. Keep the places with a 1: 32, 8, 4 and 1.
- Step 3: Add: 32 + 8 + 4 + 1 = 45.
Answer: 45
- Example 2
Decimal to binary
Write the decimal number 89 in binary.
Show the solutionHide the solution
- Step 1: The largest power of 2 that fits in 89 is 64. Put a 1 in the 64s place. 89 − 64 = 25 left.
- Step 2: 32 doesn't fit in 25: 0 in the 32s place. 16 fits: 1, leaving 9.
- Step 3: 8 fits in 9: 1, leaving 1. 4 doesn't fit: 0. 2 doesn't fit: 0. 1 fits: 1, leaving 0.
- Step 4: Read the bits from the 64s place down: 1 0 1 1 0 0 1. Check: 64 + 16 + 8 + 1 = 89.
Answer: 1011001
- Example 3
The bits trap
A game stores each player's score as a whole number using 8 bits. What is the largest score it can store, and what kind of error happens if a player with that score earns 1 more point? A student says that adding a 9th bit would let the game store scores up to 256. Explain the mistake.
Show the solutionHide the solution
- Step 1: 8 bits give 2⁸ = 256 patterns. Starting from 0, they cover 0 to 255, so the largest score is 255 (binary 11111111).
- Step 2: 255 + 1 = 256, which needs 9 bits (100000000). It doesn't fit in 8 bits, so this is an overflow error.
- Step 3: A 9th bit doubles the number of patterns to 2⁹ = 512, so scores could go up to 511, not 256. Adding a bit doubles the range; it doesn't add one.
Answer: The largest is 255; adding 1 causes an overflow error. A 9th bit would allow scores up to 511.
Common mistakes
- Starting place values at 1 on the right but doubling wrong (1, 2, 4, 6...). Each place is exactly double the one to its right: 1, 2, 4, 8, 16.
- Saying n bits hold values up to 2ⁿ. The count of values is 2ⁿ, but starting from 0 the largest is 2ⁿ − 1.
- Thinking more bits always fixes round-off. More bits make approximations closer, but many real numbers still can't be stored exactly.
- Assuming the exam's pseudocode has an overflow limit. Its whole numbers are limited only by memory.
On the exam
- Expect to convert between binary and decimal by hand, often to compare or order a mix of binary and decimal values. Convert everything to decimal first, then compare.
- Questions on the number of bits needed (for IDs, colors or characters) are common. Find the smallest n with 2ⁿ at least as big as the number of values.
Connected topics
Videos
Check yourself
4 questions on 2.1 Binary Numbers. Pick an answer to see if you got it, and why.
What is the decimal (base 10) value of the binary number 101101?
Which of the following is the binary (base 2) representation of the decimal number 38?
Which of the following binary numbers has the greatest value?
Which of the following lists the binary numbers 1010, 1100 and 0111 from least to greatest?
0 of 4 answered