AP Computer Science Principles · Study guide

Binary Numbers

AP® Computer Science Principles · Big Idea 2 · Data

Binary Numbers

How computers store every value as bits, how to convert between binary and decimal, how many values a group of bits can hold, and what goes wrong when the bits run out.

1Bits and bytes

Computing devices store data digitally: whatever a value is, at the lowest level it is made of bits. Bit is short for binary digit, and a bit is either 0 or 1. A byte is 8 bits.

Nobody writes programs one bit at a time. Programs work with data values, which can be stored in variables, in lists of items, or in standalone constants, and can be passed into a procedure as input or come back out as output:

score ← 87
grades ← [87, 92, 78]
MAX_SCORE ← 100
PROCEDURE percent(points)
{
  RETURN(points * 100 / MAX_SCORE)
}
DISPLAY(percent(grades[2]))

This displays 92. The name score lets you think “87” without ever seeing the bits 01010111 that store it. That is abstraction: reducing complexity by focusing on the main idea, hiding the details that don't matter for the question at hand and bringing together the ones that do.

2Same bits, different meanings

Bits are grouped to represent abstractions such as numbers, characters (letters, digits, symbols), and colors. The same sequence of bits can mean different things in different contexts. The byte 01000001 is the number 65 to a program reading a number, the letter A to a program reading text, and a dark red level to a program reading a pixel.

Figure 1. Nothing inside the bits says what they are. The program that reads them decides.

A color on a screen is usually three bytes: how much red, green, and blue light to mix. Orange is (255, 165, 0), stored as 11111111 10100101 00000000. The computer stores the bits; a program turns them into something a person can understand, like a colored dot or the word “orange.”

3Analog data and sampling

Analog data change smoothly over time instead of jumping in steps: the pitch and volume of music, the colors in a painting, the position of a sprinter during a race. A computer cannot store “smooth”; it can only store bits. So it samples: it measures the analog signal at regular intervals, and each measurement (a sample) is stored using a fixed number of bits.

Figure 2. Measuring at regular intervals turns a smooth signal into a list of numbers. More samples, or more levels per sample, follow the curve more closely and take more bits.

Here each sample is rounded to one of 8 levels, so 3 bits per sample are enough: the samples are stored as 100, 110, 110, 101, 101, and so on. Using digital data to approximate real-world analog data is another example of abstraction: the samples keep the main shape and drop the detail between them.

4Binary place value

A number base is how many digits a number system uses. Decimal (base 10) uses the digits 0 to 9. Binary (base 2) uses only 0 and 1. Both work by place value: where a digit sits decides what it is worth.

Positions are numbered from the right, starting at 0. The place value of a position is the base raised to the power of the position. In decimal that gives 1, 10, 100, 1000; in binary it gives 1, 2, 4, 8, 16, and so on, doubling each step to the left. A digit's value is the digit times its place value, so in binary only the 1s count.

Position76543210
Place value1286432168421
Written as power2726252423222120
Figure 3. Each position is worth a power of two, starting with 1 on the right. Only the 1s count, so 10110101 is 181.

5Converting both ways

Binary to decimal. Write the place values over the bits, skip every 0, and add the place values over the 1s. For 10110101: 128 + 32 + 16 + 4 + 1 = 181.

Decimal to binary. Start with the largest power of two that is not bigger than the number. Go down the powers one at a time: if it fits in what is left, write 1 and subtract it; if not, write 0. Here is 45:

Power of twoLeft to placeFits?WriteLeft after
3245yes113
1613no013
813yes15
45yes11
21no01
11yes10

Reading the last column of 1s and 0s from top to bottom gives 101101. Check by converting back: 32 + 8 + 4 + 1 = 45.

6Comparing binary numbers

To compare two binary numbers, line them up on the right (put zeros in front of the shorter one), then read from the left. The first position where they differ decides: the number with the 1 there is larger. Or convert both to decimal.

Never count 1s. 10000000 (128) is larger than 01111111 (127) even though it has fewer 1s, because its one 1 sits in the 128 place, which is worth more than all seven places to its right put together.

7How many values fit

Each bit doubles the number of different patterns. With n bits there are 2n patterns, so they can stand for the whole numbers 0 up to 2n − 1 (all ones).

BitsDifferent patternsWhole numbers they can hold
120 to 1
240 to 3
4160 to 15
82560 to 255
1665,5360 to 65,535
324,294,967,2960 to 4,294,967,295

To find the fewest bits for k different values, find the smallest n with 2n at least k. For 1,000 values: 29 = 512 is too few, 210 = 1,024 is enough, so 10 bits.

Because each bit doubles the count, going from 8 bits per pixel to 24 bits per pixel does not give 3 times as many colors: it gives 224 ÷ 28 = 216 = 65,536 times as many.

8When the bits run out

In many programming languages a whole number (an integer) gets a fixed number of bits. That limits which values it can hold and which calculations work. A result too big for its bits causes an overflow error. With 8 bits the largest value is 255, so 200 + 100 = 300 does not fit; many systems keep only the lowest 8 bits and end up with 44, a wrong answer and no warning.

A real case: many computers count time as seconds since 1 January 1970 in a signed 32-bit integer, whose largest value is 2,147,483,647. That count runs out at 03:14:07 on 19 January 2038 (UTC). One second later an unfixed system reads the date as 1901.

Other languages hide this limit: an integer can grow as large as the computer's memory allows. The pseudocode reference language works this way, so integers in it never overflow:

x ← 1
REPEAT 70 TIMES
{
  x ← x * 2
}
DISPLAY(x)

This displays 1180591620717411303424 (270), far beyond what 32 or 64 bits can hold.

Real numbers (numbers with a fractional part) also get a fixed number of bits, and many of them, such as 0.1, cannot be written in binary without rounding. They are stored as close approximations, which causes round-off errors. In Python and JavaScript, 0.1 + 0.2 gives 0.30000000000000004, and adding 0.1 ten times gives 0.9999999999999999, not 1.

Overflow errorRound-off error
Happens tointegers (whole numbers)real numbers (decimals)
Causethe value is too large for the fixed number of bitsthe value cannot be stored without rounding in the bits available
What you seea badly wrong value, often a wrap-around to a small or negative numbera tiny difference from the exact answer, which can build up
The key idea

Everything a computer stores is bits, and the same bits can mean different things. In binary each position is worth a power of two; n bits hold 2n values. A fixed number of bits limits integers (overflow) and real numbers (round-off); the reference language's integers are limited only by memory.

9Worked examples

Worked Example 1 Binary to decimal

Read the place values

Convert 11001010 to decimal.

  1. 1
    Place values from the left: 128, 64, 32, 16, 8, 4, 2, 1.
  2. 2
    The 1s sit under 128, 64, 8, 2.
  3. 3
    128 + 64 + 8 + 2 = 202.
Answer: 202
Worked Example 2 Decimal to binary

Largest power first

Convert 77 to binary.

  1. 1
    The largest power of two not bigger than 77 is 64.
  2. 2
    Go down the powers:
    Power of twoLeft to placeFits?WriteLeft after
    6477yes113
    3213no013
    1613no013
    813yes15
    45yes11
    21no01
    11yes10
  3. 3
    Check: 64 + 8 + 4 + 1 = 77.
Answer: 1001101
Worked Example 3 Count the bits

Student ID numbers

A school with 1,300 students wants every student to have a different ID made of bits. What is the fewest number of bits? What happens if the school grows past the limit?

  1. 1
    210 = 1,024, which is fewer than 1,300.
  2. 2
    211 = 2,048, which is enough.
  3. 3
    With 11 bits, student number 2,049 would have no pattern left. One more bit doubles the room to 4,096.
Answer: 11 bits
Worked Example 4 Order binary numbers

Smallest to largest

Put these in order from smallest to largest: 1010, 111, 1001, 10001.

  1. 1
    Pad with zeros on the left so they line up: 01010, 00111, 01001, 10001.
  2. 2
    Read from the left. Only 10001 has a 1 in the 16 place, so it is largest. 00111 has a 0 in the 8 place, so it is smallest.
  3. 3
    01001 and 01010 first differ in the 2 place, and 01010 has the 1 there.
  4. 4
    In decimal: 111 = 7, 1001 = 9, 1010 = 10, 10001 = 17.
Answer: 111, 1001, 1010, 10001
Worked Example 5 Name the error

Overflow, round-off, or neither?

(a) A game written in a language with 16-bit integers keeps adding 1 to a score that has reached 65,535. (b) A program adds 0.1 to a total ten times and compares the total with 1. (c) The same score counter, written in the pseudocode reference language.

  1. 1
    (a) 16 bits hold 0 to 65,535, so 65,536 does not fit: overflow. With wrap-around the score becomes 0.
  2. 2
    (b) 0.1 is stored as an approximation; the total is 0.9999999999999999, so the comparison with 1 is false: round-off.
  3. 3
    (c) The reference language's integers are limited only by memory, so the score keeps going up: no error.
Answer: (a) overflow, (b) round-off, (c) neither

10Common mistakes

Common Mistake 1 · Starting place values at the left

The rightmost bit is position 0 and is worth 1. Write the place values from the right: 1, 2, 4, 8, … then read them left to right.

Common Mistake 2 · Comparing by counting 1s

1000 (8) is larger than 0111 (7). The leftmost difference decides, not how many 1s there are.

Common Mistake 3 · Thinking one more bit adds one value

Each extra bit doubles the number of patterns. 9 bits hold 512 patterns; 10 bits hold 1,024.

Common Mistake 4 · Mixing up overflow and round-off

Overflow: a whole number too big for its bits. Round-off: a decimal that has to be rounded to fit. A program can have one without the other.

Common Mistake 5 · Forgetting that n bits stop at 2n − 1

8 bits make 256 patterns, but since one of them is 0, the largest value is 255, not 256.

Explain a binary result like this

“In 11001010, the 1s are in the 128, 64, 8, and 2 places, so its value is 128 + 64 + 8 + 2 = 202.”

Name the place values that hold a 1, then add them.