Number Bases
Robert P. Webber and Don Blaheta, Longwood University
Place-value notation
Why are we working on this?
In our ultimate goal of representing all data, we need to take
everything we've figured out how to represent as numbers—and things
that were numbers in the first place—and put it in a form the computer
can use. For pure human use, decimal is convenient, but binary form
will work best for computers. Hexadecimal is a compromise: easily
converted to binary for computer use but somewhat easier for humans to
work with.
Skills in this section:
Translate positive integers freely among bases 2, 10, and 16
Concepts:
Data representation,
Classic algorithms,
Limitations of representation
Our civilization uses the base 10 or
decimal place-value system. There are
ten different digits, 0-9, so a one-digit number in this system can
represent ten possible different values. But by putting those same
ten digits in different positions in a multi-digit number, we can
represent arbitrarily large and small numbers, limited only by the
number of digits we're willing to use for the number.
We call it a
place-value
system because the exact value that a digit represents is determined
in part by where in a number it is found. The digit 7 represents
exactly 7 in a number like 347, but it represents 70 in a number
like 374, it represents 70,000 in a number like 3871524, and it
represents
in a number like 32.74. In a multi-digit number, each place, or
position, is associated with a different power of ten. If there is
no decimal point (we'll deal with those in detail a bit later), the
rightmost digit is the "ones" place, to its left the "tens" place,
then "hundreds" and so on.
That means that any time we write a number in the
usual way, we are implying that each digit is multiplied by the
appropriate power of ten, and the results are added together. So a
number like 3574 means
3574 =
3×1000
+ 5×100
+ 7×10
+ 4×1
We can make the "powers of ten" aspect of this more explicit by
writing them with exponents:
3574 =
3×103
+ 5×102
+ 7×101
+ 4×100
This general pattern holds no matter how large the number gets; we can
just keep moving to higher powers of ten.
It is worth taking a moment to highlight the
importance of the digit zero here. While 0 does in some sense mean
"nothing", we still have to write it when it's in the middle of a
number like 305. Why? Because our place-value system means that
305 =
3×102
+ 0×101
+ 5×100
and in this kind of a notation we
need a zero in the tens
place to explicitly say "no tens".
In a place-value system there's really no way to directly
abbreviate
3×100
+ 5×1
and the number 35 is certainly not the same thing. (As an aside:
There
are number systems that do not use place-value
notation; you've probably seen Roman numerals, which write 305 as
CCCV:
305 =
100 + 100 + 100 + 5
so they
don't need an explicit zero, but Roman numerals
turn out to be inconvenient for a lot of other reasons.)
What we've seen so far is mostly review of what you learned about
numbers in elementary school, but with an added layer of notation
that may seem needlessly complex. The reason for that extra
notation is to set us up to understand that 10 may be important for
historical reasons (and is probably used in part due to the
physiology of our hands), but there's nothing mathematically
inevitable about using powers of ten for our place-value system.
Now we'll see what happens when we use other bases to represent
numbers.
Binary numbers
One of the most important number bases other than 10 is
base 2, also called
binary. Just as base 10 uses ten
digits (0-9) to represent numbers, base 2 uses two digits: 0
and 1. Mathematically, it's important
because 2 is the smallest possible base for a place-value system,
and mathematics is often about reducing complex systems and problems
to more minimal systems. From an electrical engineering standpoint,
binary numbers are a good foundation for computers because it's
easier to design and build systems where a wire is either "on" or
"off" like a light switch—two possible values—rather than having to
distinguish three or four or ten different values on the same wire.
So let's apply our understanding of decimal notation
to see how binary notation would work. We know we only have the
digits 0 and 1 to work with, so what would a binary number like 1101
represent? Following our pattern from before:
1101 =
1×23
+ 1×22
+ 0×21
+ 1×20
If we fill in more conventional representations for the powers of
two we have
1101 =
1×8
+ 1×4
+ 0×2
+ 1×1
And in our standard decimal notation, 8+4+1 = 13.
How do we even know that a number like 1101 is in binary? In the
preceding paragraph, it was explicitly identified as such, but
without some context, it could just as well be decimal (representing
one thousand, one hundred and one). As a matter of convention, when
working with numbers that may be in different numeric bases, we will
often specify the base of a particular number as a subscript after
the number. So
11012
is a base 2 number (as analysed above), while
110110
is a base 10 number (one thousand, one hundred and one).
The binary representation of a number is no better or worse than
the decimal representation, and while it's correct to say that
11012
represents the number
1310,
it is equally true to say that
1310
represents the number
11012.
Both are just different ways of writing the same number, although
the decimal representation will be more familiar and intuitive to
most of us, since we've been working with base ten a lot longer!
And because humans will generally find decimal more useful
even when some computer application may call for binary,
it's important to be able to convert between the two
representations.
In our explanation of what binary notation is, we've
already seen how to convert from binary to decimal, but here is
another example:
Example: Convert
10110102 to base ten.
Solution: Write out the full place-value interpretation of
each digit, and solve from there.
10110102
|
= |
1×26
+ 0×25
+ 1×24
+ 1×23
+ 0×22
+ 1×21
+ 0×20
|
|
= |
1×64
+ 0×32
+ 1×16
+ 1×8
+ 0×4
+ 1×2
+ 0×1
|
|
= |
64 + 16 + 8 + 2
|
|
= |
90
|
So
10110102
=
9010.
Converting in the other direction is a matter of reversing this
process,
filling in blanks in
the binary place value chart, starting as far left as possible. That
is, knowing that a binary number deals with powers of two, you can set
up the place-value notation with blanks for the unknown parts, and fill
in from there. Reversing the order of the above example:
9010
|
= |
?×64
+ ?×32
+ ?×16
+ ?×8
+ ?×4
+ ?×2
+ ?×1
|
|
= |
1×64
+ 0×32
+ 1×16
+ 1×8
+ 0×4
+ 1×2
+ 0×1
|
|
= |
1×26
+ 0×25
+ 1×24
+ 1×23
+ 0×22
+ 1×21
+ 0×20
|
|
= |
10110102
|
How do you know how to fill in the blanks? Start with
out the largest power of 2 that "fits" in the original number.
Subtract it from the original number, and repeat with the remainder.
Continue until the remainder is 0.
Example: Convert 10210 to binary.
Solution:
64 ( = 2
6) is the largest power of 2 that is less than
or equal to 102. Put 1 in the 64’s place and subtract it off.
10210
|
= |
1×64
+ ?×32
+ ?×16
+ ?×8
+ ?×4
+ ?×2
+ ?×1
(102−64 = 38 remaining)
|
Repeat, this time using 38. The largest power of 2 less than or
equal to 38 is 32 (=2
5). Put 1 in the 32’s position
and subtract it off.
10210
|
= |
1×64
+ 1×32
+ ?×16
+ ?×8
+ ?×4
+ ?×2
+ ?×1
(38−32 = 6 remaining)
|
Continuing,
6 – 4 = 2. Put 1 in the 4’s position.
10210
|
= |
1×64
+ 1×32
+ ?×16
+ ?×8
+ 1×4
+ ?×2
+ ?×1
(6−4 = 2 remaining)
|
Because we have bypassed the 16's place and the 8's place, the rules
of place-value notation require us to fill them in with zeroes:
10210
|
= |
1×64
+ 1×32
+ 0×16
+ 0×8
+ 1×4
+ ?×2
+ ?×1
(still 2 remaining)
|
The remaining 2 lets us put a 1 in the 2's place, and then zeroes in
any further positions (in this case just the 1's place):
10210
|
= |
1×64
+ 1×32
+ 0×16
+ 0×8
+ 1×4
+ 1×2
+ 0×1
|
|
= |
11001102
|
Try
counting in binary, starting with 0. Do you get 0, 1, 10, 11, 100,
101, 110, 111, 1000, …?
What is the next number after 1111 in binary? Why?
Hexadecimal (base 16) numbers
Just as we use base 10 and base 2 in place-value notation,
we could use other bases.
In particular, base 16, also called hexadecimal, is
used frequently in computer science.
Since
162 = 256, 163 = 4096, and so on, hexadecimal
place values increase rapidly!
Let's jump right in and apply the principles we
learned earlier, with binary, to convert a base 16 number into
base 10.
Example: Convert 15816 to decimal.
Solution:
15816
|
= |
1×162
+ 5×161
+ 8×160
|
|
= |
1×256
+ 5×16
+ 8×1
|
|
= |
256 + 80 + 8
|
|
= |
34410
|
It's important to remember that just because 158 (without subscript
or context) looks
just like how we write one hundred and
fifty-eight, since we have said it is a hexadecimal representation
of a number, the places mean different things and we need to use
some conversion process to interpret its value into our more
familiar decimal notation.
15816 ≠ 15810.
We're still missing an important piece of hexadecimal notation,
though. We've already seen that base 10 has ten distinct
digits (0-9) that it can use in each position, and base 2 has
two (0 and 1). We can then infer—correctly—that base 16 needs
to be able to use sixteen different digits to work; because
if 1016 is
1×16+0 or 1610, we need to have
something to represent the numbers that are greater than 9 but less
than 1016.
The standard convention in this case is to use the first six
letters of the alphabet as digits representing the numbers 10-15.
So when you add one to 9, in hexadecimal, you end up with A. Add
another one and you get B. If you add one to F16, which
corresponds to 1510, you get 1016, which
corresponds to 1610.
So let's work out an example that makes use of one of
these new digits.
Example: Convert 10A316 to base 10.
Solution:
10A316
|
= |
1×163
+ 0×162
+ A×161
+ 3×160
|
|
= |
1×4096
+ 0×256
+ 10×16
+ 3×1
|
|
= |
4096
+ 160
+ 3
|
|
= |
425910
|
This example was carefully selected to involve easy arithmetic (like
multiplying by ten), but you might notice that these numbers are in
danger of becoming very unwieldy if we wanted to convert a number
like 7C9
16. Hold that thought; a little later we'll see
another way to convert from hexadecimal to decimal that might be a
little easier.
The importance of hexadecimal to computer science is that
although it may be less familiar than decimal, it is much easier for
humans to work with than binary; and unlike decimal, it is
very easy to convert hexadecimal into binary (and back).
Because 16 is itself a power of 2, conversion between base 16
and base 2 can use an algorithm that doesn't work for bases
that don't have that relationship.
Each digit in a hexadecimal number corresponds
exactly to four digits (bits) in a binary number. Thus to convert
from hexadecimal into binary, you can proceed digit-by-digit rather
than having to do any division or subtraction or anything like that.
Example: Convert C60516 to base 2.
Solution:
Expand each hexadecimal digit to its four-bit binary form. First,
C
16 is 12
10, which (since it is 8+4) we can
turn into the four-bit binary sequence 1100
2:
C60516
|
= |
1100 ... 2
(605 remaining)
|
6 is 4+2, so it is 110 in binary, but because each hexadecimal digit
corresponds to
four binary digits, we must write it as
0110:
C60516
|
= |
1100 0110 ... 2
(05 remaining)
|
We process the digits 0 (can't skip it!) and 5 in the same way:
C60516
|
= |
1100 0110 0000 01012
|
It is also correct to write the answer without spaces, as
1100011000000101
2,
which is a good reminder that inside the computer there are no
"spaces" in this sense. However, when binary numbers are to be
processed by humans, they're just a lot easier to work with if
they're broken up a little bit (for the same reason we often write a
comma to separate groups of three digits in a decimal number).
Because of the importance of groups of four, the binary digits are
nearly always grouped that way; indeed, one way to think of
hexadecimal (and the reason it's so important in computer science)
is as a more compact way of writing groups of four bits. That is,
"7" in this context is just a fancy way of writing "0111", and "D"
just a fancy way of writing "1101".
Converting in the other direction essentially
reversing this process with the only distinction being that the
grouping should be done right-to-left so that the incomplete group
of four, if any, comes first.
Example: Convert 10110110012 to hexadecimal.
Solution:
Group the bits by fours, starting on the right. For emphasis, write
two leading zeros to round out the first group of bits.
10110110012
|
= |
10 1101 10012
|
|
= |
0010 1101 10012
|
|
= |
2D916
|
Earlier we mentioned an easier way to convert from hexadecimal to
decimal (and we never even showed converting in the other
direction). Basically, because the conversion between base 16
and base 2 is so easy, in all but the simplest cases
it's almost always easier to do a decimal-hexadecimal conversion (in
either direction) by first converting to binary as an intermediate
step. That way you never have to multiply by anything but zero and
one, and you don't have to separately keep track of powers of
sixteen.
The process works in both directions, but first let's
use it for the direction we've already seen.
Example: Convert 3C516 into decimal notation.
Solution:
3C516
|
= |
0011 1100 01012
|
|
= |
1×29
+ 1×28
+ 1×27
+ 1×26
+ 0×25
+ 0×24
+ 0×23
+ 1×22
+ 0×21
+ 1×20
|
|
= |
1×512
+ 1×256
+ 1×128
+ 1×64
+ 0×32
+ 0×16
+ 0×8
+ 1×4
+ 0×2
+ 1×1
|
|
= |
512
+ 256
+ 128
+ 64
+ 4
+ 1
|
|
= |
96510
|
Again, working in the other direction is a matter of
reversing the direction of the process.
Example: Convert 64310 into hexadecimal notation.
Solution:
64310
|
= |
1×512
+ ... (643−512 = 131 remaining)
|
|
= |
1×512
+ 1×128
+ ... (131−128 = 3 remaining)
|
|
= |
1×512
+ 1×128
+ 1×2
+ ... (3−2 = 1 remaining)
|
|
= |
1×512
+ 1×128
+ 1×2
+ 1×1
|
|
= |
1×29
+ 1×27
+ 1×21
+ 1×20
|
|
= |
1×29
+ 0×28
+ 1×27
+ 0×26
+ 0×25
+ 0×24
+ 0×23
+ 0×22
+ 1×21
+ 1×20
|
|
= |
10 1000 00112
|
|
= |
28316
|
Don't forget that since hexadecimal numbers don't
have to
contain the letter-digits A-F, a number written as 283 can be a
perfectly good hexadecimal number (though it does not represent two
hundred and eighty three). It's especially important in cases like
this to indicate clearly that the number is hexadecimal, through a
subscript or some other notation.
Exercises
- Convert the binary numbers to decimal.
- 10110
- 11100111
- Convert the decimal numbers to binary.
- 86
- 131
- Convert the base 10 numbers to hexadecimal.
- 68
- 543
- 127
- Convert the hexadecimal numbers to decimal.
- 10D
- 345
- BEEF
- Convert the base 16 numbers to binary.
- 53
- 94B0
- 3ED
- Convert the binary numbers to hexadecimal.
- 1010 0101
- 0011 0000 1101 1111
- 1001 0111 0110 1000
- Convert the base 10 number 36 to
- binary
- hexadecimal
- Convert the binary number 1000 1010 to
- hexadecimal
- decimal
- Convert the base 16 number 3C5 to
- binary
- base 10
Credits and licensing
This article is by Robert P. Webber and Don
Blaheta, licensed under a
Creative
Commons BY-SA 3.0 license.
Version 2017-Jan-14 18:45