5) Bases
5) Bases
Justin Wyss-Gallifent
1
1 Introduction and Base 10
What does it mean to use base 10? It means that when we write a number,
such as 639, the 9 is in the 1s place, the 3 is in the 10s place, and the 6 is in the
100s place.
If we think of 1 = 100 , 10 = 101 , and 100 = 102 then we are saying:
63910
2 Base 8 (Octal)
In general a number in base 8 has the expression:
n is written as ...d3 d2 d1 d0
Where 0 ≤ di ≤ 7 and:
n = ... + d3 · 83 + d2 · 82 + d1 · 81 + d0 · 80
Example 2.1. For example the expression 462 in base 8 represents the
number:
462 = 4 · 82 + 6 · 81 + 2 · 80 = 30610
When we want to be completely clear that ...d3 d2 d1 d0 is in base 8 we’ll subscript
the number with 8, for example the above would be:
4628 = 30610
2
3 Base b
In general a number in base b has the expression:
n is written as ...d3 d2 d1 d0
Where 0 ≤ di ≤ b − 1 and:
n = ... + d3 · b3 + d2 · b2 + d1 · b1 + d0 · b0
Example 3.1. For example the expression 235 in base 6 represents the
number:
2356 = 2 · 62 + 3 · 61 + 5 · 60 = 8510
Observe that when b > 10 the “digits” would need to be 10 and larger. Typically
what is done is that after 9 we continue with A, B, C, etc.
4 Base 16 (Hexadecimal)
In general a number in base 16 has the expression:
n is written as ...d3 d2 d1 d0
Where di ∈ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F } and we have the following,
where A is interpreted as 10, B as 11, and so on:
Example 4.1. For example the expression 4EA in base 16 represents the
number:
5 Base 2 (Binary)
In general a number in base 2 has the expression:
n is written as ...d3 d2 d1 d0
Where 0 ≤ di ≤ 1 and:
3
n = ... + d3 · 23 + d2 · 22 + d1 · 21 + d0 · 20
Example 5.1. For example the expression 110101 in base 2 represents the
number:
1101012 = 1 · 25 + 1 · 24 + 0 · 23 + 1 · 22 + 0 · 21 + 1 · 20 = 5310
1597 − 3(512) = 61
61 − 7(8) = 5
4
7 Base 10 to Base b the Fast Way
There’s a slicker, more organized way to go about this. Let’s to back to our
example of 9789 which we’d like in base 8.
What we do is start with 9789 and repeatedly divide by 8, each successive time
using the previous quotient as the dividend and continuing until the quotient is
0:
9789 ÷ 8 = 1223 R 5
1223 ÷ 8 = 152 R 7
152 ÷ 8 = 19 R 0
19 ÷ 8 = 2 R 3
2 ÷ 8 = 0R2
978910 = 230758
This looks a bit like magic so it’s worth taking a second to see what this calcu-
lation has actually told us. If we rewrite the successive divisions above:
9789 = 1223 · 8 + 5
= (152 · 8 + 7) · 8 + 5
= 152 · 82 + 7 · 8 + 5
= (19 · 8 + 0) · 82 + 7 · 8 + 5
= 19 · 83 + 0 · 82 + 7 · 8 + 5
= (2 · 8 + 3) · 83 + 0 · 82 + 7 · 8 + 5
= 2 · 84 + 3 · 83 + 0 · 82 + 7 · 8 + 5
5
20075 ÷ 16 = 1254 R 11
1254 ÷ 16 = 78 R 6
78 ÷ 16 = 4 R 14
4 ÷ 16 = 0 R 4
To convert to hexadecimal simply group the bits into groups of four and convert
each group directly to octal. Make sure to group from right to left and prepend
0s if necessary. We may have to think of an intermediate decimal value for each
group but the values are small.
6
9 Octal and Hexadecimal to Binary by Group-
ing
In these cases we can simply reverse the above procedure.
10 Base n Addition
We add in base n just as we do in decimal. When we add digits we have to
make sure that the resulting number is in the correct base and we must carry
when appropriate. This requires that we can convert relatively small numbers
into the necessary base but this is usually pretty easy to do.
7
1
2 3 4
4 0 3
1 1 4 2
Thus:
2345 + 4035 = 11425
When we do hexadecimal we have to be careful with our sums because of A
through F .
Binary is a little easier since there are only a few possible sums. Here’s an
example without all the details:
8
Example 10.3. Here is 11001010112 + 01101100112 .
1 1 1 1
1 1 0 0 1 0 1 0 1 1
0 1 1 0 1 1 0 0 1 1
1 0 0 1 1 0 1 1 1 1 0