CIS 150 Number Systems Notes

Objectives

  • List all the digits of a particular base
  • Convert from any base to base 10
  • Convert from base 10 to any base
  • Convert from base 2 to base 16
  • Convert from base 16 to base 2
  • Identify the number base for binary, hexadecimal, octal, and decimal
  • Describe why base 2 and 16 are used with computers

Introduction

We take our number system for granted. After it is taught in elementary school, people don't usually give it much more thought. Programmers do have to think about number systems, though, because computers use a different number system than we do. This lesson is intended to be a brief introduction to number systems in general, and to the number systems we use with computers in particular.

Different Number Bases

The number system we use is base 10, which is also known as decimal. One common theory is that we use base 10 because that is how many fingers most people have. Whatever the reason, using base 10 means that we group things into 10s. One consequence of this grouping number is that we must be able to count objects up to 10. We have to have symbols to represent every possible amount less than our grouping number. This is why we use the digits 0 through 9. If we used a different base for our number system, we would need a different set of digits.

One good question is what digits other bases would have. Well, binary (base 2) groups by 2s, so it only needs the digits 0 and 1. Octal (base 8) uses the digits 0 through 7. We have already seen that our decimal (base 10) system uses the digits 0 through 9.

What about number systems with a base higher than 10? In particular, let's look at hexadecimal (base 16). Base 16 needs symbols to count from 0 to 15. The numbers 10, 11, 12, 13, 14, and 15 do not suffice because they are two characters each. Our number systems are 'positional'. This means that where a symbol (digit) is placed conveys meaning. This is obvious when we look at the numbers 19 and 91 in base 10. There is quite a bit of difference between 19 and 91. We know this because of the relative positions of the digits. We can't use two characters to represent 10 through 15 objects. We need a single symbol for each of them.

We could make up unique symbols for the numbers we have to be able to represent, but that presents some problems. We could agree that a star meant 10 items, a cowboy hat meant 11, and so on. Personally, I would have trouble remembering what each symbol meant and I would prefer not to have to draw a cowboy hat every time I wanted to symbolize 11 in base 16. There is also the problem of entering this information into a computer. From time to time, people have to enter hexadecimal numbers into the computer. My computer keyboard doesn't have a cowboy hat key. What's the solution?

Fortunately, this was all worked out long ago. It was decided that when we were dealing with a number base greater than 10, we would start using the letters of the alphabet, (in order) when we ran out of numeric digits. It may seem odd at first, but it actually becomes fairly easy to figure out which letter stands for what quantity. In decimal, the numbers count up like this: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. In hexadecimal, the numbers count up like this: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. In hexadecimal, the A symbolizes a grouping of what we know as 10 objects. B represents a grouping of what we think of as 11 objects. This continues up to F, which represents what we think of as 15 objects. Remember that the digits must stop one short of the number base. That is why F (15) is the largest digit in base 16.

For reference, here are the common number systems in use with computers, listed with their digits:

binary(base 2)0, 1
octal(base 8)0, 1, 2, 3, 4, 5, 6, 7
decimal(base 10)0, 1, 2, 3, 4, 5, 6, 7, 8, 9
hexadecimal(base 16)0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

Why bother?

You may be wondering why we use different number systems with computers? It was mentioned earlier that we may use base 10 because of the number of fingers we usually have. It was for convenience. When we look at computers at a very low level, we find that they are built out of transistors. These are little electronic switches (like light switches) that are either on or off. We usually shorten the notation to 0 (off) and 1 (on). You will even see power switches and buttons on computers labeled with a 0 and 1 sometimes. This stands for off and on.

Everything we do in computers is based on those little switches which we represent as 0 or 1. Since a computer is composed of literally millions of these switches, our representation of the computers internals involves millions of 0s and 1s at this low level. These switches, which are on or off, can be thought of as counting from 0 to 1. If a signal is absent, the switch counts 0. If a signal is present, it counts 1. This is a binary (base 2) number system. This is why we use other number systems with computers. It is the nature of the beast.

Well, that explained (hopefully) why we might use base 2, but why base 8 or 16? It turns out that base 2 becomes unwieldly very quickly. If I want to represent what we think of as 34983, I would have to write it in binary as 1000100010100111. The smaller the base, the longer the strings of digits become. It turns out that it is fairly easy to convert between base 2 and bases 8 or 16 (and vice versa). This is because they are all powers of 2. It is much easier to deal with many computer problems if we just work with base 8 or 16. In fact, base 8 is not used as often anymore, so we will concentrate on base 16.

Positional significance

It was mentioned earlier that our number system is positional. The rightmost position represents a single items. The position to the left of that represents the number of groups of ten that there are. If there are more than nine groups of ten, we have to go to a third position to indicate the number of groups of "ten groups of ten" (one hundred). All of the positions relate back to the number base we are using.

There is a formula that tells you what any position means in any base. It is based upon the power of a number. Just in case you've forgotten, the power of a number is how many times that number is multiplied times itself. The power is represented by a superscript number. For example:

102 = 10 * 10 = 100 34 = 3 * 3 * 3 * 3 = 81

Each position in a particular base corresponds to a power of the base. It starts with the rightmost digit being the base number raised to the power of 0. The digit to its left is the base number raised to the power of 1. The next digit to the left is the base to the power of 2, etcetera. This is usually easier to demonstrate with examples:

Converting to base 10

Let's take base 10 (decimal) as our example to start with. Let's also take a sample number to see how this works. The number we will examine is 4735. It should also be mentioned that any number to the power of 0 is defined to be 1.

4 7 3 5 103 102 101 100 100 = 1 (by definition) 101 = 10 (10) 102 = 100 (10 * 10) 103 = 1000 (10 * 10 * 10)

What the above diagram signifies is that the number 4735 in decimal stands for 4  1000s plus 7  100s plus 3   10s plus 5  1s. When we add them all up, we find out what the equivalent number is in base 10. In this case 4000 + 700 + 30 + 5 = 4735. No surprise there. Want to see how this works with numbers from other bases? Of course you do!

Let's take 47358. The subscript tells us what base the number is in. If there is no subscript, we assume base 10. Let's pull this number apart.

4 7 3 5 83 82 81 80 80 = 1 (by definition) 81 = 8 (8) 82 = 64 (8 * 8) 83 = 512 (8 * 8 * 8)

What the above diagram signifies is that the number 4735 in octal (base 8) stands for 4  512s plus 7  64s plus 3   8s plus 5  1s. When we add them all up, we find out what the equivalent number is in base 10. In this case 2048 + 448 + 24 + 5 = 2525. Therefore, 47358 = 252510.

With a little practice, you should be able to convert from any other base to base 10. Let's do two more quick examples using the two other bases we are likely to see in computers.

Problem: convert 101101012 to base 10 1 0 1 1 0 1 0 1 27 26 25 24 23 22 21 20 20 = 1 21 = 2 22 = 4 23 = 8 24 = 16 25 = 32 26 = 64 27 = 128 Equivalent number in base 10 is (1 * 128) + (0 * 64) + (1 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (1 * 1) = 128 + 32 + 16 + 4 + 1 = 18110

The last conversion to base 10 problem involves base 16 (hexadecimal). Remember that in base 16 we needed extra digits, so A stands for 10, B stands for 11, and so forth.

Problem: convert 20D516 to base 10 2 0 D 5 163 162 161 160 160 = 1 161 = 16 162 = 256 163 = 4096 Equivalent number in base 10 is (2 * 4096) + (0 * 256) + (13 * 16) + (5 * 1) = 8192 + 208 + 5 = 840510

Converting from base 10 to other bases

Converting from base 10 into other bases is the reverse process to what we were just doing. We take the number in base 10 and start trying to figure out how many of the big powers we can subtract from our number. We keep doing that until we are finished. This is best done with an example.

Problem: convert 252510 to base 8 84 = 4096 (8 * 8 * 8 * 8) 83 = 512 (8 * 8 * 8) 82 = 64 (8 * 8) 81 = 8 (8) 80 = 1 (by definition) How many 4096s do we need? 0 (4096 is greater than 2525) 2525 - (0 * 4096) = 2525 left How many 512s do we need? 4 (5 would be too many) 2525 - (4 * 512) = 2525 - 2048 = 477 left How many 64s do we need? 7 477 - (7 * 64) = 477 - 448 = 29 left How many 8s do we need? 3 29 - (3 * 8) = 29 - 24 = 5 left How many 1s do we need? 5 5 - (5 * 1) = 5 - 5 = 0 left We must always end up with 0. If we take the numbers we answered to each of the questions above, the answer is 47358

Let's try another problem. Let's convert 18110 to base 2.

Problem: convert 18110 to base 2 28 = 256 27 = 128 26 = 64 25 = 32 24 = 16 23 = 8 22 = 4 21 = 2 20 = 1 How many 256s do we need? 0 (256 is greater than 181) 181 - (0 * 256) = 181 left How many 128s do we need? 1 181 - (1 * 128) = 181 - 128 = 53 left How many 64s do we need? 0 53 - (0 * 64) = 53 left How many 32s do we need? 1 53 - (1 * 32) = 53 - 32 = 21 left How many 16s do we need? 1 21 - (1 * 16) = 21 - 16 = 5 left How many 8s do we need? 0 5 - (0 * 8) = 5 left How many 4s do we need? 1 5 - (1 * 4) = 5 - 4 = 1 left How many 2s do we need? 0 1 - (0 * 2) = 1 left How many 1s do we need? 1 1 - (1 * 1) = 1 - 1 = 0 left We must always end up with 0. If we take the numbers we answered to each of the questions above, the answer is 101101012

Let's take a look at converting a number in base 10 to base 16 (hexadecimal). Let's convert 840510 to base 16.

Problem: convert 840510 to base 16 164 = 65536 (16 * 16 * 16 * 16) 163 = 4096 (16 * 16 * 16) 162 = 256 (16 * 16) 161 = 16 (16) 160 = 1 (by definition) How many 65536s do we need? 0 (65536 is greater than 8405) 8405 - (0 * 65536) = 8405 left How many 4096s do we need? 2 8405 - (2 * 4096) = 8405 - 8192 = 213 left How many 256s do we need? 0 213 - (0 * 256) = 213 left How many 16s do we need? 13 (D) 213 - (13 * 16) = 213 - 208 = 5 left How many 1s do we need? 5 5 - (5 * 1) = 5 - 5 = 0 left We must always end up with 0. We have one slight problem. The answer to the fourth question is 13. Each of the answers has to be a digit in the number system we are converting to. Remember that in hex, A=10, B=11, C=12, D=13, E=14, and F=15. Therefore, the answer to that question becomes D. If we take the numbers we answered to each of the questions above, the answer is 20D516

Converting between arbitrary bases

Converting a number between two arbitrary bases is a two-step process, but you already know how to do both steps. Convert the number from whatever base it is in to base 10. Then convert the number from base 10 into whatever base you want it to be in.

This example demonstrates how to convert a number in base 8 (octal) into a number in base 16 (hexadecimal).

Problem: convert 738 to base 16 81 = 8 80 = 1 738 = (7 * 8) + (3 * 1) = 56 + 3 = 5910 162 = 256 161 = 16 160 = 1 5910 = (0 * 256) + (3 * 16) + (11 * 1) = 3B16 Therefore, 738 = 3B16