# Numbers in Different Bases

### Changing to base 10 from another base

When we write a normal (base 10) number, like 5763, we mean the value:

$$5000 + 700 + 60 + 3$$

or, to put it in a more revealing form:

$$5 \cdot 10^3 + 7 \cdot 10^2 + 6 \cdot 10^1 + 3 \cdot 10^0$$

Notice, the "digits" of our number correspond to the coefficients on the powers of ten that are added together to obtain the value of our number.

In a similar manner, we can specify numbers in other "bases" (besides 10), using different digits that correspond to the coefficients on the powers (of the given base) that must be added together to obtain the value of our number.

For example, the "base 8" (or "octal") number (as indicated by the subscript)

$$5763_{(8)}$$

equals

$$5 \cdot 8^3 + 7 \cdot 8^2 + 6 \cdot 8^1 + 3 \cdot 8^0 = 3059$$

More generally, the "base b" number

$${d_n d_{n-1} d_{n-2} \ldots d_0} _{(b)}$$

equals

$$d_n \cdot b^{n} + d_{n-1} \cdot b^{n-1} + d_{n-2} \cdot b^{n-2} + \cdots + d_0 \cdot b^0$$

In order that every number have a base b representation, but no number has more than one such representation, we must only use the digits 0 through (b-1) in any given base b number.

This is consistent with base 10 numbers, where we use digits 0-9.

For smaller bases, we use a subset of these digits. For example, in base 5, we only use digits 0-4; in base 2 (which is also called binary), we only use the digits 0 and 1.

For larger bases, we need to have single digits for values past 9. Hexadecimal (base 16) numbers provide an example of how this can be done. In hexadecimal, we use digits 0-9 and A-F, where A=10, B=11, C=12, D=13, E=14, and F=15. In this way, we have digits corresponding to 0-15, which is what we need.

Example

$$5AF8 = 5 \cdot 16^3 + 10 \cdot 16^2 + 15 \cdot 16^1 + 8 \cdot 16^0 = 23288$$

(Note: As shown above, the subscript indicating the base to be used is often left off in the case of hexadecimal and/or binary numbers. In these instances, the context of their use usually makes the base clear.)

### Changing from base 10 to a different base

One (straight-forward, but inefficient) way to convert from base 10 to a different base is to:

1. Determine the higest power of the base that goes into the number a non-zero number of times.
2. Determine how many times this power can be subtracted from the number without the result being negative (i.e, divide the number by the power). Write this digit down.
3. Redefine the number to be this smallest positive remainder upon division by the power in question
4. Redefine the power to be the power divided by the base.
5. Go back to step 2, unless the power is now less than one -- in which case, you are done.

For example, to convert 1073 to base 5, we recall that:

50 = 1
51 = 5
52 = 25
53 = 125
54 = 625
55 = 3125


Then we notice that 54 = 625 is the highest power of 5 under 1073.

1073 = 1 * 625 + 448
448 = 3 * 125 + 73
73 = 2 * 25 + 23
23 = 4 * 5 + 3
3 = 3 * 1 + 0


The red digits, 13243, reveal the base 5 representation of 1073.

This process, however, is inefficient in that one must both know and use the various powers of the desired base.

There is a simpler way!

Consider the remainders seen upon division of the following numbers by 5:

1073 = 214 * 5 + 3
214 = 42 * 5 + 4
42 = 8 * 5 + 2
8 = 1 * 5 + 3
1 = 0 * 5 + 1


Note: the base 5 representation comes from reading off the remainders (in red) from bottom to top!
In each step above, we are just dividing by 5 and looking at both the quotient and remainder -- no knowledge of higher powers of 5 is necessary!

Wonderfully, this technique works in any base. (Can you explain why?)

So, for example, if we wanted to find the binary (base 2) representation of 1000, we simply calculate the following:

1000 = 500 * 2 + 0
500 = 250 * 2 + 0
250 = 125 * 2 + 0
125 = 62 * 2 + 1
62 = 31 * 2 + 0
31 = 15 * 2 + 1
15 = 7 * 2 + 1
7 = 3 * 2 + 1
3 = 1 * 2 + 1
1 = 0 * 2 + 1


So 1000 in binary is 1111101000

### Counting in another base

Counting in other bases is not too different from counting in base 10. To see the similarities, let's count to 41 in base 10 and base 3 (as shown in the table below).

Pay particular attention to how "2" in base 3 plays the same role as "9" in base 10. It represents the last digit you can use before increasing the digit to the immediate left.

Base 10   Base 3   Base 10   Base 3
0 0 21 210
1 1 22 211
2 2 23 212
3 10 24 220
4 11 25 221
5 12 26 222
6 20 27 1000
7 21 28 1001
8 22 29 1002
9 100 30 1010
10 101 31 1011
11 102 32 1012
12 110 33 1020
13 111 34 1021
14 112 35 1022
15 120 36 1100
16 121 37 1101
17 122 38 1102
18 200 39 1110
19 201 40 1111
20 202 41 1112

You can add in another base (without converting to base 10) as long as you remember that you "carry" when you have a sum that is greater than or equal to your base (instead of greater than or equal to 10), and that what you "carry" is the number of times you can pull out the base from your sum.

This is best illustrated by an example. Suppose you wish to add the hexadecimal numbers 4EF5A and 6ACF7:

  1111    <---- These are the "carried" digits
4EF5A
+6ACF7
------
B9C51


Let's walk through the example. Notice that

 A + 7 = 11                    (hexadecimal calculations)
10 + 7 = 17 = 1 * 16 + 1       (decimal calculations)


So we write down a 1 in the "units" column and carry a 1. Then,

1 + 5 + F = 15                 (hexadecimal calculations)
1 + 5 + 15 = 21 = 1 * 16 + 5   (decimal calculations)


So we write down a 5 in the "tens/sixteens" column and carry a 1. Then,

1 + F + C = 1C                 (hexadecimal calculations)
1 + 15 + 12 = 28 = 1 * 16 + 12 (decimal calculations)


So we write down a C in the next column and carry a 1. Then,

1 + E + A = 19                (hexadecimal calculations)
1 + 14 + 10 = 25 = 1 * 16 + 9 (decimal calculations)


So we write down a 9 in the next column and carry a 1. Then,

1 + 4 + 6 = B                 (hexadecimal calculations)
1 + 4 + 6 = 11                (decimal calculations)


So we write down a B in the next column, and we are done.

### Shortcut for changing between base 2 and base 16

Consider the following conversion from binary to hexadecimal:

100100010101111 (binary) = 0100 1000 1010 1111
4    8   10    15
=   4    8    A    F

Hence 10010001010111 (binary) = 48AF (hex)


Amazingly, one can always break a binary number into groups of 4 digits (starting at the right, and adding leading zeros if one runs out of digits), and then reinterpreting these groups of 4 as hexadecimal values, arrive at the hexadecimal representation for the original binary number. (Can you figure out why?)

Reversing the process is just as easy.

Suppose we wish to convert FC7 (hex) to binary form. Note that

F (hex) = 15 = 1111 (binary)
C (hex) = 12 = 1100 (binary)
7 (hex) =  7 = 0111 (binary)

Hence, FC7 (hex) = 111111000111 (binary)