Number Bases

 Converting between different bases is actually fairly simple, but the thinking behind it can seem a bit confusing at first.  And while the topic of different bases may seem somewhat pointless to you, computers and computer graphics have made much more important the knowledge of how to work with different (non-decimal) base systems, particularly binary (ones and zeroes) and hexadecimal (the numbers zero through nine, followed by the letters A through F).

In our customary base-ten system, we have digits for the numbers zero through nine.  We do not have a single-digit numeral for "ten".  Yes, we write "10", but this stands for "1 ten and 0 ones".  This is two digits; we have no single solitary digit that stands for "ten".

Instead, when we need to count to one more than nine, we zero out the ones column and add one to the tens column.  When we get too big in the tens column -- when we need one more than nine tens and nine ones ("99"), we zero out the tens and ones columns, and add one to the ten-times-ten, or hundreds, column.  The next column is the ten-times-ten-times-ten, or thousands, column.  And so forth, with each bigger column being ten times larger than the one before.  We place digits in each column, telling us how many copies of that power of ten we need.

The only reason base-ten math seems "natural" and the other bases don't is that you've been doing base-ten since you were a child.  And (nearly) every civilization has used base-ten math probably for the simple reason that we have ten fingers.  If instead we lived in a cartoon world, where we would have only four fingers on each hand (count them next time you're watching TV or reading the comics), then the "natural" base system would likely have been base-eight, or "octal".

Binary

Let's look at base-two, or binary, numbers.   How would you write, for instance, 1210 ("twelve, base ten") as a binary number?  You would have to convert to base-two columns, the analogue of base-ten columns.  In base ten, you have 100 = 1, 101 = 10, 102 = 100, 103 = 1000, and so forth.  Similarly in base two, you have 20 = 1, 21 = 2, 22 = 4, 23 = 8, 24 = 16, and so forth.

The first column in base-two math is the units column.  But only "0" or "1" can go in the units column.   When you get to "two", you find that there is no single solitary digit that stands for "two" in base-two math.  Instead, you put a "1" in the twos column and a "0" in the units column, indicating "1 two and 0 ones".  The base-ten "two" (210) is written in binary as 102.

A "three" in base two is actually "1 two and 1 one", so it is written as 112.  "Four" is actually two-times-two, so we zero out the twos column and the units column, and put a "1" in the fours column; 410 is written in binary form as 1002.

Here is a listing of the first few numbers:

decimal
(base
10)
binary
(base
2)
 

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

0
1
10
11
100
101
110
111
1000
1001
1010
1011
1100
1101
1110
1111
10000

0 ones
1 one
1 two and zero ones
1 two and 1 one
1 four, 0 twos, and 0 ones
1 four, 0 twos, and 1 one
1 four, 1 two, and 0 ones
1 four, 1 two, and 1 one
1 eight, 0 fours, 0 twos, and 0 ones
1 eight, 0 fours, 0 twos, and 1 one
1 eight, 0 fours, 1 two, and 0 ones
1 eight, 0 fours, 1 two, and 1 one
1 eight, 1 four, 0 twos, and 0 ones
1 eight, 1 four, 0 twos, and 1 one
1 eight, 1 four, 1 two, and 0 ones
1 eight, 1 four, 1 two, and 1 one
1 sixteen, 0 eights, 0 fours, 0 twos, and 0 ones

Converting between binary and decimal numbers is fairly simple, as long as you remember that each digit in the binary number represents a power of two.  For instance:

List the digits in order, and count them off from the RIGHT, starting with zero:

digits:   1  0   1  1  0  0  1  0  1
numbering:   8  7   6  5  4  3  2  1  0

Use this listing to convert each digit to the power of two that it represents:

1×28 + 0×27 + 1×26 + 1×25

         + 0×24 + 0×23 + 1×22 + 0×21 + 1×20

= 1×256 + 0×128 + 1×64 + 1×32

        + 0×16 + 0×8 + 1×4 + 0×2 + 1×1

= 256 + 64 + 32 + 4 + 1

= 357

Then 1011001012 converts to 35710.

Converting decimal numbers to binaries is nearly as simple:  just divide by 2.

To do this conversion, you need to divide repeatedly by 2, keeping track of the remainders as you go.  

As you can see, after dividing repeatedly by 2, I ended up with these remainders:

1  R  0

2 |  2  R 1 

2 |  5  R 1 

2 | 11 R 0 

2 | 22 R 0 

2 | 44 R 1 

2 | 89 R 0 

2 |178 R 0

2 |357  

These remainders tell us what the binary number is!  Read the numbers from around the outside of the division, starting on top and wrapping your way around the right-hand side.  As you can see:

35710 converts to 1011001012.

You can convert from base-ten (decimal) to any other base.  When you study this topic in class, you will probably be expected to convert numbers to various other bases, so let's looks at a few more examples.

Base 4

In base four, each digit in a number represents the number of copies of that power of four.  That is, the first digit tells you how many ones you have; the second tells you how many fours you have; the third tells you how many sixteens (four-times-fours) you have; the fourth tells you how many sixty-fours (four-times-four-times-fours) you have; and so on.  The methodology for conversion between decimal and base-four numbers is just like that for decimals and binaries, except that binary digits can be only "0" or "1", while the digits for base-four numbers can be "0", "1", "2", or "3".  (As you might expect, there is no single solitary digit in base-four math that represents the quantity "four".)

Do the same division that you did before, keeping track of the remainders.  (You may want scratch paper for this.)

 1 R 1 

4 |   5 R 2 

4 | 22 R 1 

4 | 89 R 1 

4 |357  

Then 35710 converts to 112114.

 3 R  0

4 |   12 R  2

4 |   50 R  1

4 |  201 R 1

4 |   807  

Note:  Once I got "3" on top, I had to stop, because four cannot divide into 3.

Reading the numbers off the division, I get that 80710 converts to 302134.

List out the digits, and then number them from the RIGHT, starting at zero:

digits:  3  0   2  1  3
numbering:  4  3   2  1  0

Now remember that each digit stands for the number of copies we need for that power of four:

3×44 + 0×43 + 2×42 + 1×41 + 3×40

= 3×256 + 0×64 + 2×16 + 1×4 + 3×1

= 768 + 32 + 4 + 3

= 807

As expected, 302134 converts to 80710.

Base Seven

I can't think of any particular use for base-seven numbers, but they will serve us by providing some more practice with conversions.

Do the division:

 1 R  0

7 |     7 R  2

7 |   51 R  0

7 |   357  

Then 35710 = 10207.

 5 R  3

7 |   38 R  6

7 |  272 R 2

7 | 1906 R1

7 | 13346  

Then 1334610 = 536247.

List the digits, and count them off from the RIGHT, starting at zero:

digits:   5  3   6  2  4
numbering:   4  3   2  1  0

Then do the multiplication and addition:

5×74 + 3×73 + 6×72 + 2×71 + 4×70

= 5×2401 + 3×343 + 6×49 + 2×7 + 4×1

= 12005 + 1029 + 294 + 14 + 4

= 13346

Then 536247 = 1334610. Copyright © Elizabeth Stap00-2004 All Rights Reserved

Octal

An older computer base system is "octal", or base eight.  The digits in octal math are 0, 1, 2, 3, 4, 5, 6, and 7.  The value "eight" is written as "1 eight and 0 ones", or 108.

Do the usual repeated division, this time dividing by 8 at each step:

 

 5 R  4

8 |   44 R  5

8 |  357  

Then the corresponding octal number is 5458.

Do the usual procedure, counting off the digits from the RIGHT, starting at zero:

digits:   5  4   5
numbering:   2  1   0

Do the addition and multiplication:

5×82 + 4×81 + 5×80

= 5×64 + 4×8 + 5×1

= 320 + 32 + 5

= 357

Then the corresponding decimal number is 35710.

Hexadecimal

If you work with computer programming or computer engineering (or computer graphics, about which more later), you will encounter base-sixteen, or hexadecimal, math.

Now, as mentioned before, decimal math does not have one single solitary digit that represents the value of "ten".   Instead, we use two digits, a 1 and a 0:  "10".  But in hexadecimal math, the columns stand for multiples of sixteen!  That is, the first column stands for how many units you have, the second column stands for how many sixteens, the third column stands for how many two hundred fifty-sixes (sixteen-times-sixteens), and so forth.

Now, in base ten, we had digits 0 through 9.  In base eight, we had digits 0 through 7.  In base 4, we had digits 0 through 3.  In any base system, you will have digits 0 through one-less-than-your-base.  Then, in hexadecimal, we have "digits" 0 through 15.

To do this, we would need single solitary digits that stand for the values of "ten", "eleven", "twelve", "thirteen", "fourteen", and "fifteen".   But we don't.  So, instead, we use letters.  That is, counting in hexadecimal, the fifteen "numerals" are:

1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

In other words, A is "ten", B is "eleven", C is "twelve", D is "thirteen", E is "fourteen", and "F" is fifteen.  It is this use of letters for digits that makes hexadecimal numbers look so odd at first.

But the conversions work in the usual manner.

Divide repeatedly by 16, keeping track of the remainders as you go.  (You might want to use some scratch paper for this.)

 

 1 R  6

16|    22 R  5

16 |   357  

Reading off the digits, starting from the top and wrapping around the right-hand side, you see that 35710 = 16516.

List the digits, and count them off from the RIGHT, starting with zero:

digits:   1  6   5
numbering:   2  1   0

Remember that each digit in the hexadecimal number represents how many copies you need of that power of sixteen, and convert the number to decimal:

1×162 + 6×161 + 5×160

= 1×256 + 6×16 + 5×1

= 256 + 96 + 5

= 357

Then 16516 = 35710.

Divide repeatedly by 16, keeping track of your remainders:

15 R   9

16 |    249 R 11

16 |  3995 R 13

  16| 63933 

From the long division, you can see that the hexadecimal number will have a "fifteen" in the sixteen-cubeds column, a "nine" in the sixteen-squareds column, an "eleven" in the sixteens column, and a "thirteen" in the ones column.  But we cannot write the hexadecimal number as "1591113", because this would be confusing and imprecise.   So we use the letters for the "digits" that are otherwise too large, letting "F" stand in for "fifteen", "B" stand in for "eleven", and "D" stand in for "thirteen".

Then 6393310 = F9BD16.

List out the digits, and count them off from the RIGHT, starting at zero:

digits:   F  9   B  D
numbering:   3  2    1  0

Actually, it will probably be helpful to redo this, converting the alphabetic "digits" to their corresponding decimal values:

digits:   15    9  11  13
numbering:     3    2   1    0

Now do the multiplication and addition:

15×163 + 9×162 + 11×161 + 13×160

= 15×4096 + 9×256 + 11×16 + 13×1

= 61440 + 2304 + 176 + 13

= 63933

As expected, F9BD = 6393310.

If you would like to try converting a decimal number to a base of your own choosing, click here.

If you would like to try converting from a base of your own choosing to a decimal number , click here.

Computer Graphics

If you work on web pages and graphics programs, you may find it helpful to convert between the RGB values (for an image in your graphics program) and the hexadecimal values (for a matching background color on the web page).

Graphics programs deal with the RGB (red-green-blue) values for colors.  Each of these components of a given color have values somewhere between 0 and 255.  These values may be converted to hexadecimal values between 00 and FF.

If you list the RGB components of a color as a string of three numbers, you might get, say, R:204, G:51, B:255, which translates into a light-purplish #CC33FF in HTML coding.  Note that 20410 = CC16, 5110 = 3316, and 25510 = FF16.

On the other hand, if you have some coding for #990033, this would translate into a dark-reddish R:153, G:0, B:51 in your graphics program.

That is, to convert between your graphics program and your web-page coding, deal with the hexadecimal number not as one six-digit number, but as three two-digit numbers, and convert these pairs of digits into the corresponding RGB values.