0% found this document useful (0 votes)
19 views9 pages

5) Bases

Uploaded by

habibulloh.al12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

5) Bases

Uploaded by

habibulloh.al12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CMSC 250: Number Bases

Justin Wyss-Gallifent

February 13, 2023

1 Introduction and Base 10 . . . . . . . . . . . . . . . . . . . . . . 2


2 Base 8 (Octal) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 Base b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4 Base 16 (Hexadecimal) . . . . . . . . . . . . . . . . . . . . . . . . 3
5 Base 2 (Binary) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
6 Base 10 to Base b the Slow Way . . . . . . . . . . . . . . . . . . 4
7 Base 10 to Base b the Fast Way . . . . . . . . . . . . . . . . . . . 5
8 Binary to Octal and Hexadecimal by Grouping . . . . . . . . . . 6
9 Octal and Hexadecimal to Binary by Grouping . . . . . . . . . . 7
10 Base n Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

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:

639 = 6 · 102 + 3 · 101 + 9 · 100

In general a number in base 10 has the expression:


n is written as ...d3 d2 d1 d0
Where 0 ≤ di ≤ 9 and:

n = ... + d3 · 103 + d2 · 102 + d1 · 101 + d0 · 100

When we want to be completely clear that ...d3 d2 d1 d0 is in base 10 we’ll subscript


the number with 10, for example:

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:

n = ... + d3 · 163 + d2 · 162 + d1 · 161 + d0 · 160

Example 4.1. For example the expression 4EA in base 16 represents the
number:

4EA16 = 4 · 162 + 14 · 161 + 10 · 160 = 125810

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

6 Base 10 to Base b the Slow Way


We’ve seen how to convert from base b to decimal. To convert from decimal to
base b. Given a decimal number n, the most obvious way is to start by finding
the largest power of b, say bi , which is less than n, then count how many of
those you can subtract from n still keeping a nonnegative result. That value is
your leftmost digit.
Next, repeat the process with bi−1 , bi−2 , and so on, adding the resulting digits
to the right until a result of 0 is obtained.

Example 6.1. Let’s convert 978910 to base 8. First we observe that 84 =


4096 and 85 = 32768 so 84 = 4096 is the largest. We can subtract two of
these:
9789 − 2(4096) = 1597
Thus our leftmost digit is 2.
Then we have 1597 left. From this we can subtract three 83 = 512s:

1597 − 3(512) = 61

Thus our next digit is 3.


Then we have 61 left. From this we can subtract zero 82 = 64s. Thus our
next digit is 0.
Then we still have 61 left. From this we can subtract seven 81 = 8s:

61 − 7(8) = 5

Thus our next digit is 7.


Now we have 5 left so the final digit is 5.
Thus:
978910 = 230758

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

We then put down the remainders in reverse order:

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

This is exactly the expansion we wanted!


Note that when we convert to hexadecimal we have to keep an eye on our digits
A through F .

Example 7.1. Let’s convert 20075 to hexadecimal.

5
20075 ÷ 16 = 1254 R 11
1254 ÷ 16 = 78 R 6
78 ÷ 16 = 4 R 14
4 ÷ 16 = 0 R 4

The “digits” 4, 14, 6, and 11 become 4E6B16 .

8 Binary to Octal and Hexadecimal by Group-


ing
In the special case when we wish to convert between binary and either octal or
hexadecimal we can do so quickly by grouping. This works specifically because
8 and 16 are powers of 2.
To convert to octal simply group the bits into groups of three and convert each
group directly to octal. Make sure to group from right to left and prepend 0s if
necessary.

Example 8.1. Let’s convert 110101000011102 to octal. We group them and


prepend a single 0 and convert:
Binary 011 010 100 001 110
Octal 3 2 4 1 6
Thus the answer is 324168 .


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.

Example 8.2. Let’s convert 111101011011102 to hexadecimal. We group


them and prepend two 0s and convert:
Binary 0011 1101 0110 1110
Decimal 3 13 6 14
Hexadecimal 3 D 6 E
Thus the answer is 3D6E16 .

6
9 Octal and Hexadecimal to Binary by Group-
ing
In these cases we can simply reverse the above procedure.

Example 9.1. Let’s convert 56108 to binary. We simply convert 5, 6, 1,


and 0 independently and string them together, yielding 101, 110, 001, and
000 so then 1011100010002 .

Example 9.2. Let’s convert A5D916 to binary. We simply convert A, 5,


D, and 9 independently and string them together, yielding 1010, 0101, 1110,
and 1001 so then 10100101111010012 .

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.

Example 10.1. Let’s do 2345 + 4035 . We line them up:


2 3 4
4 0 3
We add 4 + 3 = 7 and in base 5 this is 12 so we put down the 2 and carry
the 1:
1
2 3 4
4 0 3
2
Then we add 1 + 3 + 0 = 4 and in base 5 this is still 4 so we put down the 4:
1
2 3 4
4 0 3
4 2
Then we add 2 + 4 = 6 and in base 5 this is 11 so we put that down:

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 .

Example 10.2. Consider 7F 2E16 + BD5C16 . We line them up:


7 F 2 E
B D 5 C
We add E + C = 1A in hexadecimal so we put down the A and carry the 1:
1
7 F 2 E
B D 5 C
A
Then we add 1 + 2 + 5 = 8 in hexadecimal so we put down the 8:
1
7 F 2 E
B D 5 C
8 A
Then we add F + D = 1C in hexadecimal so we put down the C and carry
the 1:
1 1
7 F 2 E
B D 5 C
C 8 A
Then we add 1 + 7 + B = 13 in hexadecimal so we put down both:
1 1
7 F 2 E
B D 5 C
1 3 C 8 A
The result is then 13C8A16 .


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

You might also like