Number Bases
Robert P. Webber and Don Blaheta, Longwood University
Placevalue 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 placevalue system. There are
ten different digits, 09, so a onedigit number in this system can
represent ten possible different values. But by putting those same
ten digits in different positions in a multidigit 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
placevalue
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 multidigit 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×10^{3}
+ 5×10^{2}
+ 7×10^{1}
+ 4×10^{0}
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 placevalue system means that
305 =
3×10^{2}
+ 0×10^{1}
+ 5×10^{0}
and in this kind of a notation we
need a zero in the tens
place to explicitly say "no tens".
In a placevalue 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 placevalue
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 placevalue 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 (09) to represent numbers, base 2 uses two digits: 0
and 1. Mathematically, it's important
because 2 is the smallest possible base for a placevalue 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×2^{3}
+ 1×2^{2}
+ 0×2^{1}
+ 1×2^{0}
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
1101_{2}
is a base 2 number (as analysed above), while
1101_{10}
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
1101_{2}
represents the number
13_{10},
it is equally true to say that
13_{10}
represents the number
1101_{2}.
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
1011010_{2} to base ten.
Solution: Write out the full placevalue interpretation of
each digit, and solve from there.
1011010_{2}

= 
1×2^{6}
+ 0×2^{5}
+ 1×2^{4}
+ 1×2^{3}
+ 0×2^{2}
+ 1×2^{1}
+ 0×2^{0}


= 
1×64
+ 0×32
+ 1×16
+ 1×8
+ 0×4
+ 1×2
+ 0×1


= 
64 + 16 + 8 + 2


= 
90

So
1011010_{2}
=
90_{10}.
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 placevalue notation with blanks for the unknown parts, and fill
in from there. Reversing the order of the above example:
90_{10}

= 
?×64
+ ?×32
+ ?×16
+ ?×8
+ ?×4
+ ?×2
+ ?×1


= 
1×64
+ 0×32
+ 1×16
+ 1×8
+ 0×4
+ 1×2
+ 0×1


= 
1×2^{6}
+ 0×2^{5}
+ 1×2^{4}
+ 1×2^{3}
+ 0×2^{2}
+ 1×2^{1}
+ 0×2^{0}


= 
1011010_{2}

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 102_{10} 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.
102_{10}

= 
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.
102_{10}

= 
1×64
+ 1×32
+ ?×16
+ ?×8
+ ?×4
+ ?×2
+ ?×1
(38−32 = 6 remaining)

Continuing,
6 – 4 = 2. Put 1 in the 4’s position.
102_{10}

= 
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 placevalue notation require us to fill them in with zeroes:
102_{10}

= 
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):
102_{10}

= 
1×64
+ 1×32
+ 0×16
+ 0×8
+ 1×4
+ 1×2
+ 0×1


= 
1100110_{2}

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 placevalue notation,
we could use other bases.
In particular, base 16, also called hexadecimal, is
used frequently in computer science.
Since
16^{2} = 256, 16^{3} = 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 158
_{16} to decimal.
Solution:
158_{16}

= 
1×16^{2}
+ 5×16^{1}
+ 8×16^{0}


= 
1×256
+ 5×16
+ 8×1


= 
256 + 80 + 8


= 
344_{10}

It's important to remember that just because 158 (without subscript
or context) looks
just like how we write one hundred and
fiftyeight, 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.
158_{16} ≠ 158_{10}.
We're still missing an important piece of hexadecimal notation,
though. We've already seen that base 10 has ten distinct
digits (09) 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 10_{16} is
1×16+0 or 16_{10}, we need to have
something to represent the numbers that are greater than 9 but less
than 10_{16}.
The standard convention in this case is to use the first six
letters of the alphabet as digits representing the numbers 1015.
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 F_{16}, which
corresponds to 15_{10}, you get 10_{16}, which
corresponds to 16_{10}.
So let's work out an example that makes use of one of
these new digits.
Example: Convert 10A3
_{16} to base 10.
Solution:
10A3_{16}

= 
1×16^{3}
+ 0×16^{2}
+ A×16^{1}
+ 3×16^{0}


= 
1×4096
+ 0×256
+ 10×16
+ 3×1


= 
4096
+ 160
+ 3


= 
4259_{10}

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 digitbydigit rather
than having to do any division or subtraction or anything like that.
Example: Convert C605_{16} to base 2.
Solution:
Expand each hexadecimal digit to its fourbit binary form. First,
C
_{16} is 12
_{10}, which (since it is 8+4) we can
turn into the fourbit binary sequence 1100
_{2}:
C605_{16}

= 
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:
C605_{16}

= 
1100 0110 ... _{2}
(05 remaining)

We process the digits 0 (can't skip it!) and 5 in the same way:
C605_{16}

= 
1100 0110 0000 0101_{2}

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 righttoleft so that the incomplete group
of four, if any, comes first.
Example: Convert 1011011001
_{2} 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.
1011011001_{2}

= 
10 1101 1001_{2}


= 
0010 1101 1001_{2}


= 
2D9_{16}

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 decimalhexadecimal 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 3C5_{16} into decimal notation.
Solution:
3C5_{16}

= 
0011 1100 0101_{2}


= 
1×2^{9}
+ 1×2^{8}
+ 1×2^{7}
+ 1×2^{6}
+ 0×2^{5}
+ 0×2^{4}
+ 0×2^{3}
+ 1×2^{2}
+ 0×2^{1}
+ 1×2^{0}


= 
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


= 
965_{10}

Again, working in the other direction is a matter of
reversing the direction of the process.
Example: Convert 643_{10} into hexadecimal notation.
Solution:
643_{10}

= 
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×2^{9}
+ 1×2^{7}
+ 1×2^{1}
+ 1×2^{0}


= 
1×2^{9}
+ 0×2^{8}
+ 1×2^{7}
+ 0×2^{6}
+ 0×2^{5}
+ 0×2^{4}
+ 0×2^{3}
+ 0×2^{2}
+ 1×2^{1}
+ 1×2^{0}


= 
10 1000 0011_{2}


= 
283_{16}

Don't forget that since hexadecimal numbers don't
have to
contain the letterdigits AF, 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 BYSA 3.0 license.
Version 2017Jan14 18:45