|


Converting BasesDate: 12/21/97 at 19:30:51 From: Linda Subject: Computer codes I'm trying to find an educational book to explain bases: hexadecimal, binary, and decimal numbers, and how to convert them. Can you recommend a book? Thank you, Linda Reinhardt
Date: 12/22/97 at 12:57:31
From: Doctor Tom
Subject: Re: Computer codes
Hi Linda,
I'd guess that almost any book on computers has a section on this.
I'm not talking about the "How to program in Basic" type books, but
rather books about how computers work.
I'll take a shot at an explanation.
First, be sure you understand exactly what numbers mean in the old,
familiar, decimal system. If I write 3409, what it really means is
("*" means multiplication):
3*1000 + 4*100 + 0*10 + 9*1
If by 10^3 I mean "ten cubed", or 10*10*10, etc., then I can also
write it as:
3*10^3 + 4*10^2 + 0^10^1 + 9*10^0
(remember that 10^0 = 1).
Now there's nothing magic about 10 - it was chosed because we happen
to have 10 fingers. If humans had 7 fingers, you can bet that we'd be
writing numbers like this:
3204 = 3*7^3 + 2*7^2 + 0^7^1 + 4*7^0
The 3204 (base 7) is NOT the same as our standard decimal base 10
3204, but the scheme above is perfectly reasonable for representing
numbers.
In base ten numbers, we need symbols for all the numbers less than 10,
so we use: "0", "1", "2", ..., "8", and "9". Once it gets bigger than
9, it slops over into the next column.
If we had 7 fingers, we would only need symbols for numbers less than
7: "0", "1", "2", "3", "4", "5", and "6". Larger numbers would slop
over into the next higher column.
So the base that you use (the examples above are in base 10 and base
7) is totally arbitrary. For technical reasons, base 10 would be an
extrememly inconvenient base to use in designing a computer. Base 2 is
much better.
So if we had only 2 fingers, we only need symbols for numbers less
than 2: "0" and "1" do the trick.
The binary number 10110 represents:
1*2^4 + 0*2^3 + 1*2^2 + 1*2^1 + 0*2^0
If you need to convert this to base ten, just work out the numbers:
2^4 = 2*2*2*2 = 16, 2^2 = 2*2 = 4, and 2^1 = 2.
So 10110 (binary) = 1*16 + 1*4 + 1*2 = 22 (decimal).
So to convert any binary number to decimal, you just need to work out
the correct powers of 2, and add them in, depending on whether there's
a 0 or a 1 in the appropriate position in the number.
Here's another example. Convert 1101101 (binary) to decimal.
This number has a 1 in the 2^0, 2^2, 2^3, 2^5 and 2^6 slots.
2^6 = 64, 2^5 = 32, 2^3 = 8, 2^2 = 4, and 2^0 = 1, so it's equal to
64+32+8+4+1 = 109 (decimal).
To convert the other way is a little trickier. Let me convert 231
(decimal) to binary. There are tricks to do this faster, but let me
show you the brute-force way to do it, since that will make it obvious
what's going on:
First, make a list of the powers of 2 that goes up high enough:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
We can stop here, since 2^8 = 256 is bigger than the number we want to
convert.
231 is bigger than 128, however, so I can write:
231 = 128 + 103
103 is bigger than 64, so I can write:
231 = 128 + 64 + 39
39 is bigger than 32, so I can write:
231 = 128 + 64 + 32 + 7
7 is bigger than 4, so I can write:
231 = 128 + 64 + 32 + 4 + 3
3 is bigger than 2, so I can write:
231 = 128 + 64 + 32 + 4 + 2 + 1
or:
231 = 1*2^7 + 1*2^6 + 1*2^5 + 0*2^4 + 0*2^3 + 1*2^2 + 1*2^1 + 1*2^0
or:
231 (decimal) = 11100111 (binary)
Try some other examples to make sure you see how this works.
As you can see, writing binary numbers takes a lot of space.
231 decimal uses only 3 digits, but the binary form takes 8 digits.
For large numbers, the binary numbers get really long.
It turns out that base 16 is also convenient, and it's just as if we
had 16 fingers. Unfortunately, we now need numbers for all the digits
less than 16, and if we stick to our standard base-10 digits, we only
have 10 of them. So we represent the numbers 10 through 15 by 10 = A,
11 = B, 12 = C, 13 = D, 14 = E and 15 = F.
So the hexidecimal (base 16) number: 2E0A means:
2*16^3 + E*16^2 + 0*16^1 + A*16^0
= 2*16^3 + 14*16^2 + 0*16^1 + 10*16^0
= 2*4096 + 14*256 + 10*1 = 8192+3584+10= 11786 (decimal)
Conversion the other way is vastly simplified because there's a
"magical" relation between binary (base 2) and base 16 numbers.
First convert your number to base 2. Suppose you get:
111101101100100000101111010000
Break it into blocks of 4 binary digits, starting from the right:
11 1101 1011 0010 0000 1011 1101 0000
If there aren't 4 digits on the left-most group, pad it out to 4 using
zeroes:
0011 1101 1011 0010 0000 1011 1101 0000
Then look up the hexidecimal digits using the following table:
0000 = 0 (These are just the binary values. For example,
0001 = 1 1011 (binary) = 11 (decimal) = B (hexidecimal).)
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = A
1011 = B
1100 = C
1101 = D
1110 = E
1111 = F
So the long binary number above is:
3DB20BD0.
I hope this helps.
-Doctor Tom, The Math Forum
Check out our web site! http://mathforum.org/dr.math/
|
Search the Dr. Math Library: |
[Privacy Policy] [Terms of Use]


Ask Dr. MathTM
© 1994-2011 The Math Forum
http://mathforum.org/dr.math/