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.
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.
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.
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Written as power | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
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 two | Left to place | Fits? | Write | Left after |
|---|---|---|---|---|
| 32 | 45 | yes | 1 | 13 |
| 16 | 13 | no | 0 | 13 |
| 8 | 13 | yes | 1 | 5 |
| 4 | 5 | yes | 1 | 1 |
| 2 | 1 | no | 0 | 1 |
| 1 | 1 | yes | 1 | 0 |
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).
| Bits | Different patterns | Whole numbers they can hold |
|---|---|---|
| 1 | 2 | 0 to 1 |
| 2 | 4 | 0 to 3 |
| 4 | 16 | 0 to 15 |
| 8 | 256 | 0 to 255 |
| 16 | 65,536 | 0 to 65,535 |
| 32 | 4,294,967,296 | 0 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 error | Round-off error | |
|---|---|---|
| Happens to | integers (whole numbers) | real numbers (decimals) |
| Cause | the value is too large for the fixed number of bits | the value cannot be stored without rounding in the bits available |
| What you see | a badly wrong value, often a wrap-around to a small or negative number | a tiny difference from the exact answer, which can build up |
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
Read the place values
Convert 11001010 to decimal.
- 1Place values from the left: 128, 64, 32, 16, 8, 4, 2, 1.
- 2The 1s sit under 128, 64, 8, 2.
- 3128 + 64 + 8 + 2 = 202.
Largest power first
Convert 77 to binary.
- 1The largest power of two not bigger than 77 is 64.
- 2Go down the powers:
Power of two Left to place Fits? Write Left after 64 77 yes 1 13 32 13 no 0 13 16 13 no 0 13 8 13 yes 1 5 4 5 yes 1 1 2 1 no 0 1 1 1 yes 1 0 - 3Check: 64 + 8 + 4 + 1 = 77.
1001101Student 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?
- 1210 = 1,024, which is fewer than 1,300.
- 2211 = 2,048, which is enough.
- 3With 11 bits, student number 2,049 would have no pattern left. One more bit doubles the room to 4,096.
Smallest to largest
Put these in order from smallest to largest: 1010, 111, 1001, 10001.
- 1Pad with zeros on the left so they line up:
01010,00111,01001,10001. - 2Read from the left. Only
10001has a 1 in the 16 place, so it is largest.00111has a 0 in the 8 place, so it is smallest. - 3
01001and01010first differ in the 2 place, and01010has the 1 there. - 4In decimal: 111 = 7, 1001 = 9, 1010 = 10, 10001 = 17.
111, 1001, 1010, 10001Overflow, 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(a) 16 bits hold 0 to 65,535, so 65,536 does not fit: overflow. With wrap-around the score becomes 0.
- 2(b) 0.1 is stored as an approximation; the total is 0.9999999999999999, so the comparison with 1 is false: round-off.
- 3(c) The reference language's integers are limited only by memory, so the score keeps going up: no error.
10Common mistakes
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.
1000 (8) is larger than 0111 (7). The leftmost difference decides, not how many 1s there are.
Each extra bit doubles the number of patterns. 9 bits hold 512 patterns; 10 bits hold 1,024.
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.
8 bits make 256 patterns, but since one of them is 0, the largest value is 255, not 256.
“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.