Uploaded by bill_larson

Bases Decimal Binary Octal Hexadecimal and all that

advertisement
Bases: Decimal, Octal, Binary, Hexadecimal and All That
William Larson, International School of Geneva
Octal (Base 8)
We all know that 52036 means
5 × 104 + 2 × 103 + 0 × 102 + 3 × 101 + 6 × 100.
But that assumes the number is base ten, that is,
in decimal. Suppose we were working in base
8, that is, in octal. Then, what does 52036
mean?
Firstly, nomenclature. There are two ways to
indicate that we are working in base 8. We can
write 520368 or 52036 OCT (or OCT 52036).
I’ll use both conventions, so that you get used
to both of them.
Example 1
Let’s count in base 8.
1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17,
20, 21, 22, 23, 24, 25, 26, 27, 30, 31, …
Notice that the numerals 8 & 9 do not exist in
base 8. In base 10 to write the number after 9
we need to use two digits. Similarly in base 8 to
write the number after 7 we need to use two
digits.
Be careful. Pronounce 238 as “two three base 8”
not twenty three”, because it’s not twenty three;
it’s 16 + 3 = nineteen!
Example 2
Convert 520368 to decimal.
= 5 × 84 + 2 × 83 + 0 × 82 + 3 × 81 + 6 × 80
= 5×4096 + 2 × 512 + 0 × 64 + 3 × 8 + 6 × 1
= 20 480 + 1024 + 0 + 24 + 6
= 2153410
We’ll adopt the standard convention that if
there is no subscript, it’s base 10.
= 21534.
Exercise 1
1. What are the next ten numbers after OCT
32?
2. Which numerals would be used for base
five?
3. Write in base 5?
a. 5
b. 25
c. 24
d. 26
e. 125
4. Convert to base 10
a) 158
b) 64158
c) 563168
5. Explain why 68158 cannot exist.
Now we’ll learn how to convert from decimal
to octal.
We need a table of powers of 8.
80 = 1
81 = 8
8² = 64
83 = 512
84 = 4096
85 = 32768
Make sure that you could have calculated these
powers by hand.
Example 3a
Convert 133510 to base 8.
Looking at the table of powers we see that
133510 is more that 8³ but less than 84, so we
need to find the numeral in the 8³ = 512th place.
Dividing 1335 ÷ 512 = 2 r. 311.
Bases: Decimal, Octal, Binary, Hexadecimal and All That
That is 1335 = 2 × 512 + 311.
So the numeral in the 512s place is 2.
1. Convert the following base 10 numbers to
Now 311 ÷ 64 = 4 r. 55.
base 8 and reconvert to check.
Finally 55 ÷ 8 = 6 r. 7.
a) 12
b) 45
Formally we should now calculate 7 ÷ 1 = 7 r.
c) 626
0, but that’s a waste of time, since the answer
d) 3294
must be the remainder from the previous
calculation.
2. Construct an octal addition table, i.e. a table
with 7 rows and 7 columns indicating, for
Picking up the numerals we get
example, that 28 + 38 = 58 and that 68 + 38
133510 = OCT 2467.
(= 910) = 118. If you are systematic, it is
easy.
Let’s check:
24678
3. Construct an octal multiplication table, i.e. a
= 2 × 83 + 4 × 82 + 6 × 81 + 7 × 80
7 × 7 table indicating, for example, that 28 ×
= 1024 + 256 + 48 + 7
38 = 68 and 68 × 38 (= 1810) = 228. Again if
= 133510
you are systematic, it is easy.
So it checks.
Another way to convert from base 10 to
another base
There is another way to convert from base 10 to
another base. It is mechanically easier, but it is
harder to see why it is correct. Just repeatedly
divide by the base and then collect the
remainders in reverse order.
Example 3b
Convert 133510 to base 8.
1335 ÷ 8 = 166 r. 7
166 ÷ 8 = 20 r. 6
20 ÷ 8 = 2 r. 4
2 ÷ 8 = 0 r. 2
Picking up the remainders in reverse order
gives 133510 = 24678, again.
4. Explain this joke: Why do machine
language computer programmers think that
Halloween is the same holiday as
Christmas? Hint: No, I have not changed
the subject.
Now we are ready to do addition and
subtraction in base 8.
Example 4
Carry out the following problem in base 8
222
+617
———
1041
Let’s examine this in detail.
2 + 7 = 9 = 118, so we wrote “1” in the ones
place and carried a 1.
Now we have 1 + 2 + 1 = 4 in the eights place
and 6 + 2 = 108, so we write 0 in the sixty-fours
place and 1 in the 512s place.
Example 5
Exercise 2
2
Bases: Decimal, Octal, Binary, Hexadecimal and All That
Carry out the following problem in base 8
735
-256
———
457
Let’s examine this in detail. We want to
subtract 6 from 5, but we cannot, so we borrow
1 which is 8 from the eights place.
8 + 5 - 6 = 7, so we write 7 in the ones place.
Now we want to subtract 5 from 2, but again
we cannot, so we borrow 1 which is 8 from the
sixty-fours place. 8 + 2 - 5 = 5, so we write 5 in
the eights place.
Then 6 – 2 = 4 so we write 4 in the sixty-fours
place.
We won’t do multiplication or fractions. Sorry.
Exercise 3
Carry out the following problems in base 8
1. 741 + 667
2. 274017 + 564637
3. 342 – 173 (Keep this one handy. We’ll
do it in class in a very special way.) 1
4. 274017 – 64637
1
http://www.youtube.com/watch?v=UIKGV2cTgqA
3
Bases: Decimal, Octal, Binary, Hexadecimal and All That
Binary (Base 2)
Binary is special for three reasons. Firstly it is
the base that all computers use internally for all
calculations of any sort, even, for example, for
word processing. Secondly, because 2³ = 8, it is
very easy to convert binary to octal and vice
versa. Thirdly sometimes we need to list all the
possible outcomes of an event which has only
two possibilities, such as "heads and tails" or
"true and false." The simplest way to be sure to
list all possibilities is to "count" in base 2.
Example 6
Let’s count in binary.
1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010,
1011, 1100, 1101, 1110, 1111, 10000, …
We need a table of powers of 2.
0
2 =1
21 = 2
2² = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128
28 = 256
29 = 512
210 = 1024
211 = 2048
212 = 4096
Incidentally base two is so imbedded into
computer technology that when you see 1 kB
that actually means 1024 (210) , not 1 000 bytes,
and 1 MB = 1 048 576 (220), not 1 000 000
bytes. Yes, really!
Example 7
Convert 10110 base 2 to decimal.
10110
= 1 × 24 + 0 × 23 + 1 × 22 + 1 × 21 + 0 × 20
16 + 0 + 4 + 2 + 0 = 22
Example 8
Convert 23 to binary.
23 = 16 + 4 + 2 + 1
= 1 × 24 + 0 × 23 + 1 × 22 + 1 × 21 + 1 × 20
= BIN 10111
Exercise 4
1. What are the next ten digits after
100002?
2. Write in base 2:
a) 32
b) 31
c) 33
d) 1 048 576
3. Convert these base ten numbers to binary
and reconvert to check.
a) 15
b) 64
c) 63
d) 57
4. Convert to base 2 numbers to decimal
and reconvert to check.
a) 11012
b) 110112
c) 10110012
Because 8 = 2³ each block of 3 binary digits
converts cleanly into one octal digit.
Example 9
Convert BIN 101 001 111 010 001 into octal
The first three digits are 101. 1012 = 510 (and
also = 58. Why?). The second three digits are
0012 = 110, etc.
So BIN 101 001 111 010 001 = OCT 51721
Example 10
Convert BIN 10 101 into octal
The first block of numerals has only 2 digits,
but that is no problem. 10 1012 = 258
4
Bases: Decimal, Octal, Binary, Hexadecimal and All That
Exercise 5
1. Convert these numbers to binary.
a) 578
b) 2138
c) 5138
a) 101012
b) 1110012
c) 1011010012
3. Explain this joke: “There are 10 kinds of
people in the world: those who
understand binary and those who don't.”
2. Convert these numbers to octal.
5
Bases: Decimal, Octal, Binary, Hexadecimal and All That
Hexadecimal (Base 16)
Example 11
Let’s count in base 16.
1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D,
1E, 1F, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
2A, 2B, 2C, 2D, 2E, 2F, 30, 31, 32, …
Huh? Where did the A, B, etc. come from?
Well in base 16 we need symbols for all of the
digits up to 16 - 1 = 15. Several sets of symbols
were proposed, but in 1963 IBM decided to use
the first 6 letters of the alphabet and the rest, as
they say, is history.
Be careful. Pronounce 2316 as “two three base
16” not twenty-three”, because it’s not twentythree; it’s 32 + 3 = thirty-five!
Here’s a table of powers of 16.
160 = 1
161 = 16
16² = 256
163 = 4096
164 = 65536
The word “bit” is short for binary digit, the
smallest unit of information on a machine. The
term was first used in 1946 by John Tukey, a
leading statistician and adviser to five
presidents.
A “byte” is equal to 8 bits. It is a unit of storage
capable of holding a single character. A byte
can have 28 different values, that is, it can take
on any of the values from 000000000 to
111111111, which are 0 to 256 in base ten or 0
to FF in hex. For example in ASCII the
character Z is represented by 5AHEX.
(See http://www.lookuptables.com/).
Unfortunately both bit and byte begin with “b”,
so their abbreviations are sometimes confused.
The abbreviations are “b” for bit and “B” for
byte. For example Mbps is short for megabits
per second. Mbps, the standard measure of
internet speed, is often (wrongly) pronounced
megabytes per second.
Example 12
Convert A4FHEX to decimal
A4FHEX
= 10 × 16² + 4 × 161 + 15 × 160
= 2560 + 64 + 15 = 2639.
Example 13
Convert 39146 to base 16.
We use the same method as when we converted
octal.
39146 ÷ 4096 = 9 r. 2282
2282 ÷ 256 = 8 r. 234
234 ÷ 16 = 14 r. 10
So we get 98EA16.
6
Bases: Decimal, Octal, Binary, Hexadecimal and All That
Exercise 6
1. What are the next ten digits after 3216?
2. Write in base 16:
a. 4096
b. 4095
c. 4097
d. 65536
3. Convert these base ten numbers to
hexadecimal and reconvert to check.
a. 53
b. 734
c. 9223
4. Convert to base 10 and reconvert to
check.
a. 4AB16
b. F7416
c. 16C0F16
5. Zombie problem: How many people can
read hex if only you and DEAD people
can read hex?
6. Show that “The Ultimate answer to Life,
the Universe and Everything”
6 × 9 = 42 is correct in base 13.
7
Bases: Decimal, Octal, Binary, Hexadecimal and All That
Answers
Exercise 1
1. 33, 34, 35, 36, 37, 40, 41, 42, 43, 44 2. 0, 1, 2, 3, 4 3.
a. 10, b. 100 c. 44 d. 101 e. 1000 4. a. 13 b. 3341 c.
23758 5. A number in octal cannot contain any digits
larger than 7.
Exercise 2
1.a. 14 b. 55 c. 1162 d. 6336
2.
1
2
3
4
5
1
2
3
4
5
6
2
3
4
5
6
7
3
4
5
6
7
10
4
5
6
7
10 11
5
6
7
10 11 12
6
7
10 11 12 13
7
10 11 12 13 14
1
2
3
4
3.
1
1
2
3
4
2
2
4
6
10
3
3
6
11 14
4
4
10 14 20
5
5
12 17 24
6
6
14 22 30
7
7
16 25 34
4. OCT 31 = DEC 25
5
5
12
17
24
31
36
43
6
7
10
11
12
13
14
15
7
10
11
12
13
14
15
16
6
6
14
22
30
36
44
52
7
7
16
25
34
43
52
61
Exercise 3
1. 1630, 2. 1060656, 3. 147, 4. 207160
Exercise 4
1. 10001, 10010, 10011, 10100, 10101, 10110, 10111,
11000, 11001, 11010
2. a. 100000, b. 11111, c. 10001, d. 1 followed by 20
zeros
3. a. 1111, b. 1000000, c. 111111, d. 111001
4. a. 13, b. 27, c. 89
Exercise 5
1. a. 101 111, b. 10 001 101, c. 101 001 011
2. a. 25, b. 71, c. 551
3. 10 in binary is two base 10
Exercise 6
1. 33, 34, 35, 36, 37, 38, 39, 3A, 3B, 3C
2. a. 1000, b. 111, c. 1001, d. 10000
3. a. 35, b. 2DE, c. 2407
4. a. 1195, b. 3956, c. 93199
5. 57006, 6. 69 = 4213 = 4  13 + 2 = 52 + 2 = 5410
8
Download