# Binary, Decimal, Hexadecimal, and All That

We count in base 10. 135 represents 1 hundred, 3 tens, and 5 ones—or 1 × 102 + 3 × 101 + 5 × 100. To get this, start with the rightmost digit; that is the number of 100s, or 1s; the next digit to the left is the number of 101s, or 10s; the next is the number of 102s, or 100s, and so on. Each digit is a non-negative integer that is less than the base; as the base is 10, the valid digits are 0, 1, 2, …, 9.

Exercise: Express the numbers 3246, 98, and 485 as the sum of powers of 10 times digits.

Computers count in binary—that is, in base 2. So the valid digits are 0 and 1. The binary number 10000111 represents 1 × 27 + 0 × 26 + 0 × 25 + 0 × 24 + 0 × 23 + 1 × 22 + 1 × 21 + 1 × 20. We did this the same way we figured out how to write 135 as the sum of powers of 10 times digits, except we used powers of 2 instead of 10.

Exercise: Express the binary numbers 1101, 101010, and 11011 as the sum of powers of 2 times digits.

This representation allows us to convert from binary to decimal. As 10000111 = 1 × 27 + 0 × 26 + 0 × 25 + 0 × 24 + 0 × 23 + 1 × 22 + 1 × 21 + 1 × 20, and 271 = 128, 22 = 4, 21 = 2, and 20 = 1, this gives

10000111 = 1 × 27 + 0 × 26 + 0 × 25 + 0 × 24 + 0 × 23 + 1 × 22 + 1 × 21 + 1 × 20 = 128 + 0 + 0 + 0 + 0 + 4 + 2 + 1 = 135

This works for any base. So the number 1020 in base 5 (which we write as 10205, the subscript 5 meaning that the number 1020 is in base 5) represents 1 × 53 + 0 × 52 + 2 × 51 + 0 × 50. To convert this number to base 10, we do this:

1020 = 1 × 53 + 0 × 52 + 2 × 51 + 0 × 50 = 125 + 0 + 2× + 0 = 125 + 0 + 10 + 0 = 135

Exercise: Express the numbers 3647 using powers of 7, 2213 using powers of 3, and 2616 using powers of 16. What is the decimal equivalent of each of these numbers?

Although computers work in binary, the human eye has trouble working with strings of 0s and 1s that are 32 or 64 in length. So people often write numbers in base 16, called “hexadecimal” or “hex” for short. It works just like the other bases we have seen. The only quirk is that 16 digits are needed (0, 1, …, 15). We write these as 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F—that is, A is the digit for 10, B the digit for 11, C the digit for 12, D the digit for 13, E the digit for 14, and F the digit for 15. So the hexadecimal number BEEF represents 11 × 163 + 14 × 162 + 14 × 161 + 15 × 160—or, in base 10:

BEEF = 11 × 163 + 14 × 162 + 14 × 161 + 15 × 160 = 11 × 4096 + 14 × 256 + 14 × 16 + 15 × 1 = 45056 + 3584 + 224 + 15 = 48879

One reason hexadecimal numbers are used is because it is very easy to convert between binary and hexadecimal. The trick is based on the fact that 16 = 24. Take your binary number and, working from the right, group the binary digits into groups of 4 (add 0s to the left if the final group doesn’t have 4 binary digits). Then look up each group inthe following table and write down the hexadecimal digit. Presto—you’re done!

binary hex binary hex binary hex binary hex
0000 0 0100 4 1000 8 1100 C
0001 1 0101 5 1001 9 1101 D
0010 2 0110 6 1010 A 1110 E
0011 3 0111 7 1011 B 1111 F

So, to convert 10000111 into binary, divide it up into groups of 4 bits. This gives 1000 0111. Now look them up in the table. 1000 corresponds to 8 and 0111 to 7. So, the number is 87 in hexadecimal.

To go from hexadecimal, take the hex number—say, 87—and use the table to convert the hexadecimal digit into binary. As 8 hex corresponds to 1000 binary, and 7 hex corresponds to 0111 binary, the 87 in base 16 is 10000111 in base 2.

Exercise: What binary number does the hexadecimal number BEEF correspond to? What hexadecimal number does the binary number 1101111010101101 correspond to?

### First Exercise

3246 = 3 × 103 + 2 × 102 + 4 × 101 + 6 × 100
98 = 9 × 101 + 8 × 100
485 = 4 × 102 + 8 × 101 + 5 × 100

### Second Exercise

1101 = 1 × 23 + 1 × 22 + 0 × 21 + 1 × 20
101010 = 1 × 25 + 0 × 24 + 1 × 23 + 0 × 22 + 1 × 21 + 0 × 20
11011 = 1 × 24 + 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20

### Third Exercise

364_7 = 3 × 72 + 6 × 71 + 4 × 70 = 3 × 49 + 6 × 7 + 4 = 147 + 42 + 4 = 193
221_3 = 2 × 32 + 2 × 31 + 1 × 30 = 2 × 9 + 2 × 3 + 1 = 18 + 6 + 1 = 25
2616 = 2 × 161 + 6 × 160 = 2 × 16 + 6 = 32 + 6 = 38

### Fourth Exercise

BEEF16 = 1011 1110 1110 11112