We count in base 10. 135 represents 1 hundred, 3 tens, and 5 ones—or 1×10^{2} + 3×10^{1} + 5×10^{0}. To get this, start with the rightmost digit; that is the number of 10^{0}s, or 1s; the next digit to the left is the number of 10^{1}s, or 10s; the next is the number of 10^{2}s, 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×2^{7} + 0×2^{6} + 0×2^{5} + 0×2^{4} + 0×2^{3} + 1×2^{2} + 1×2^{1} + 1×2^{0}. 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×2^{7} + 0×2^{6} + 0×2^{5} + 0×2^{4} + 0×2^{3} + 1×2^{2} + 1×2^{1} + 1×2^{0}, and 2^{7} = 128, 2^{2} = 4, 2^{1} = 2, and 2^{0} = 1, this gives

10000111 | = | 1×2^{7} + 0×2^{6} + 0×2^{5} + 0×2^{4} + 0×2^{3} + 1×2^{2} + 1×2^{1} + 1×2^{0} |

= | 128 + 0 + 0 + 0 + 0 + 4 + 2 + 1 = 135 |

1020 | = | 1×5^{3} + 0×5^{2} + 2×5^{1} + 0×5^{0} |

= | 25 + 0 + 2×5 + 0 = 125 + 0 + 10 + 0 = 135 |

*Exercise*:
Express the numbers 364_{7} using powers of 7, 221_{3} using powers of 3, and 26_{16} 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×16^{3} + 14×16^{2} + 14×16^{1} + 15×16^{0}—or, in base 10:

BEEF | = | 11×16^{3} + 14×16^{2} + 14×16^{1} + 15×16^{0} |

= | 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 = 2^{4}$. 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 in table 1 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?

98 = 9×10

485 = 4×10

101010 = 1×2

11011 = 1×2

221

26

1101111010101101

A PDF version is available here.

ECS 10, Basic Concepts of Computing Fall Quarter 2012 |