If you like the style of Practical Guides, let Niels help you understand Operating Systems in his newest book:
A Guide to Operating Systems: Troubleshooting and Problem Solving
Practical Guide to Binary, Decimal and Hexadecimal Numbers
...because it doesn't have to be complex.
by Niels Jonker
Copyright © 1997 Niels Jonker
All Rights Reserved
You are looking at a Draft that has not yet been edited!
DISCLAIMER: This guide on numbers was written to support a series of other documents about various Internet protocols. It is not meant to be the ultimate complete source of everything there is to know; all I want to do is refresh or introduce the concepts used in other documents.
You are undoubtedly familiar with the decimal numbering system, which is why we will start with it. After all, starting with something you already know will make it easy o introduce the concepts we will use in the other numbering systems.
The Decimal system is what you use everyday when you count/ Its name is derived from the Latin word Decem, which means ten. This makes sense since the system uses ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. These digits are what we call the symbols of the decimal system.
Since we have ten symbols, we can count from 0 to 9. Note that 0, even though it often means 'nothing', is a symbol that counts! After all, you need a numeric way to say 'nothing'. When you want to count past what your simple symbols will allow, you combine multiple digits. The table below shows this concept, which is demonstrated by adding one for every step:
The table has 10 numbers across, which is the same number of symbols as the decimal system. As you look at row 2, you notice that we added symbol 1 to the 0, making 10. In row 3, the one is replaced by a 2, giving 20. The further you go down the table, the higher the numbers get. You could say that the first row of the table really has a leading 0, after all the 'value' of 01 is the same as the value of one. All 2-digit decimal numbers can be put in a somewhat larger table much like the one above, which looks like this:
Least Significant Digit
This shows the concept of the Least Significant Digit or the LSD, which is the digit on the right. As you can see, it the digit that increases every time we increase the number. When we run out of LSD's, we increase the digit to the left of it. In this example, it is the Most Significant Digit or the MSD. When we run out of MSD's, we add another digit to the left, again by increasing the imaginary 0 there.
For example, after 98, we increase the LSD to get 99, which is the same as 099. When we add one, the LSD rolls to 0, so we increase the next digit over to the left (The current MSD), which now becomes 0, which means we roll the next digit over (The 0), which gives us 100. Note that the MSD is now the 1.
As a general rule, the significance of the digits becomes greater as we move to the left, towards the MSD. This is why when we count the digits in a number, we always count them from the Right hand side (LSD) to the Left Hand Side (MSD). In the decimal number 346981, we say that 1, the LSD, is digit number 0. The 9 is digit number 2, and the three is digit number 5. No matter what system, decimal, binary or hexadecimal, we always count digits from right to left, LSD will always be on the right, and we will always express the digit number in decimal.
Just like we assume in normal life that things are done in Decimal, we will do everything in Decimal as well. All the math that is to follow is performed in decimal, unless specifically otherwise mentioned. Since decimal is the normal numeric system, there is no special notation used to identify decimal numbers.
Although it is valid to think of decimal numbers as having a large number of 0's to the left of the MSD, and we will at times write these 0's down, you must be careful when entering numbers in a computer. Some programs will interpret a leading zero in front of a number to mean "This is not decimal". You have been warned! Also, as we all know, negative numbers exist in real life. In our explanation, we do not need them, and therefor we are hereby setting the rule that negative numbers will not be used; we will only use the number 0 and up. To make matters even simpler, we will only allow whole numbers. Even though there are fractions and decimal numbers in real life, we also do not need them. So what we are left with are all whole numbers 0 and up. Mathematicians call these Natural Numbers; computer scientists call them unsigned integers. Remembering that you should only see Natural Numbers (whole numbers 0 and up) can help you find mistakes.
Now that we have looked at the Decimal Number System, let's take a quick look at traditional, decimal math. The six operations we are concerned with are addition, subtraction, multiplication, integer division, modulo and exponents. We will discuss each shortly:
Addition: The process of adding two numbers. This is done by simply increasing one number with the other: 2+3=5, because if you count three steps up from the digit two you get to the digit five. 7+5=12, because when when you count five steps up from seven you get 12.
Subtraction: The process of subtracting one number from another. This is done by simply decreasing one number by the number of steps indicated by another: 5-3=2, because if you count three steps down from 5 you get two. 12-5=7 because when you count five steps down from twelve, you get to 7.
So, Subtraction is in essence the exact reverse operation of addition. The only thing that is different is the direction in which the counting happens.
Multiplication: The result of the process of repeatedly adding numbers. This is done by repeated addition of a number to itself: 2*3=6 because 2+2+2=6. 8*6=48 because 8+8+8+8+8+8=48. Obviously, 5*0=0, because if you add no fives, you get nothing, and 5*1=5, because one five is simply 5.
Integer Division: The inverse process of Multiplication. In principle, this is done by repeated subtraction of a number from another number: 6/2=3, because 6-2-2-2=0, you subtract two three times to get to 0, 48/8=6 because 48-8-8-8-8-8-8=0. Note that division does not always 'work' this way: 20/8 does not fit the mold, because 20-8-8=4, and therefore we would need a 'part of 8'. We are only interested in whole number, so in cases like this we will say that 20/8 = 2, with a remainder of 4. This remainder is what is called the Modulo. Obviously, division by 20/1=20 because you only need one 20 to make 20, and the modulo will always be 0. Division by 0 is not possible; after all no matter how often zero is subtracted, the number will never decline. Note that this is a special case where Integer Division is not the reverse of multiplication. Note that Division By Zero Is Impossible And Has No Answer, impossible means the answer is not 0 or infinitely large, as is often wrongly thought. Since you have to divide to calculate the modulo, anything modulo 0 is also an invalid operation. Note especially that 0 may be divided by anything; 0/anything is always zero. The modulo will always be zero as well.
Notice that the processes of Multiplication is no more than a mathematical shorthand for the repetition of the process of Addition.
Exponents: The result of the process of repeated multiplication of a number by itself. 2^3=8 because 2*2*2=8 because 2*(2+2)=8 because (2+2)+(2+2)=8. 3^4=81 because 3*3*3*3=81 because 3*3*(3+3+3)=81 because 3*((3+3+3)+(3+3+3)+(3+3+3))=81 because ((3+3+3)+(3+3+3)+(3+3+3))+((3+3+3)+(3+3+3)+(3+3+3))+((3+3+3)+(3+3+3)+(3+3+3))=81. A special case is the exponent 0. By definition, any number^0=1. This is an exception to the rules listed previously, the reason why this is important will become clear later on.
Just like Multiplication is Math Shorthand for the result of repeated Addition of a number to itself, Exponents are shorthand for the result of repeated Multiplication of a number by itself.
Here again are the symbols we will use to represent these mathematical operations:
Note that these symbols are considered 'standard', they are what many modern programming languages (Such as C++ and Java) and Application Software (Such as Spreadsheets) use. When some of these operators are combined with numbers, we get an expression. Expressions are evaluated in a certain order; all of the exponents are computed first, followed by the multiplications, divisions and modulos, followed by the additions and subtractions. 4+2*3=10 because 2*3=6 and 4+6=10. Also, 4+3*2^2=16 because 2^2=4 and 3*4=12 and 4+12=16. 2^2*3+4=16 as well, for the same reason.
If on the other hand you wanted to first add 4 and three, then multiply that by 2, and then calculate the square, you have to explicitly describe that process by using parenthesis. Anything inside parenthesis is looked upon as a separate expression, which is evaluated first. For our example, you would have to write: ((4+3)*2)^2, which will be evaluated as 4+3=7 and 7*2=14 and 14^2=196.
There are numerous other Math operations, and some of the ones discussed here would not be so simple to explain had we not set the rule that we will only use Unsigned Integers / Natural Numbers. Whatever you do, keep in mind that this is not the complete guide to everything there is to know about math.
Now that we have some math in our hands, let's look at Decimal numbers again. Take the number 7 for example. What can we say about it? It has only one digit (7), which is both the Most Significant Digit and the Least Significant Digit. The digit is in Position 0. The value it expresses is 7, and it is a decimal number.
A mathematical way of describing 7 would be to say that it represents the number of ones in the decimal world. 7 simply means seven ones, or 7*1.
Looking at the number 36, we can do the same thing: It has 2 digits (3 and 6), 3 is the most significant digit, 6 is the least significant digit. The LSD is in position 0, the MSD is in position 1. The value it represents is 36, and it is a decimal number.
A mathematical way of describing 36 would be to say that it it represents three tens, and six ones in the decimal world, or 3*10 + 6*1. Three teens and six ones make thirty-six.
1369 works the same: 4 digits, 1 is MSD, 9 is LSD, MSD in position 3, LSD in position 0, value is 1369 in Decimal. Or, mathematically: one thousand, three hundreds, 6 tens and 9 ones in the decimal world, or 1*1000 + 3*100 + 6*10 + 9*1.
If you take a look at the values (1000, 100, 10, 1) by which we multiply the digits (1, 3, 6, 9) and the relationship to the digit positions (3, 2, 1, 0), and combine this with the knowledge that this the decimal number set, you could come up with the following math to describe the above number in a different way: 1*10^3 + 3*10^2 + 6*10^1 + 9*10^0.
Similarly, 36 can be described as 3*10^1 + 6*10^0, and 7 as 7*10^0. Before you move on, it is important that you understand the math here; without it you will get lost when we die in to the hexadecimal and binary number set.
In principal, Hexadecimal numbers work in the exact same way as decimal numbers do. Nearly all the same rules apply. The main difference is that there are more symbols. In the case of Hexadecem, Latin for 16, there are 16. The first ten are old faithful 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. The six new ones are A, B, C, D, E, F. Just like we made a little counting table for Decimal, we can make one for Hexadecimal as well:
Apart from the difference in the symbols and the different number of them, the Math rules in Hexadecimal, which we will from now on simply call Hex, are the same as the rules in Decimal.In Hexadecimal math, 4+1=5 and 6+3=9 and 9+3=C. As in Decimal math, you add an MSD to the left in Hex when you run out of Symbols, example in Hex: C+5=11 and Hex 11+3C=4D. Although this sounds simple and logical, our brain has trouble with it since we are trained in decimal! A calculator can be handy in cases like this.
The rule for Decimal numbers holds true for Hex numbers for a far as the mathematical representation of the digits that make up a number. In Hex math, the following is true:
D1C5 = D*10^3 + 1*10^2 + C*10^1 + 5*10^0
The complete table for all 2-digit Hex numbers is obviously larger then the one for Decimal, since we have more symbols, there are more possible combinations. Given 10 symbols in Decimal, that table had 100 elements (10 rows by 10 columns). The hex table will thus have 256 elements (16 rows by 16 columns):
|Least Significant Digit|
From now on, we will identify hexadecimal numbers in either of two ways: By putting an H behind them (Such as 12H) or by putting 0x in front (Such as 0x12, standard C++ notation). The Decimal Math Functions work the same on Hexadecimal Numbers. Because the symbol set is different and larger, you will however get different results most of the time. For example: 10+10=20 and 10H+10H=20H, but 50+50=100 and 50H+50H=A0H!
As with Hexadecimal numbers, Binary numbers are just a different set of symbols, but all the same math applies. Bi is Latin for two; the binary number set only has two symbols, 0 and 1. The counting table for Binary is very simple indeed:
As you probably had expected, the Math works the same, although the numbers get really big due to the limited symbol set. In Binary math: 1+1=10 and 110+101=1011. The Mathematical Representations are also still the same. Again in Binary:
10 = 1*10^1+0*10^0
We will append a B to the end of Binary numbers from now on, as in 1011B and 100B. All standard Math functions work in Binary. Binary Digits are sometimes called Bits. Numbers consisting of four bits are known as nibbles, and numbers of 8 bits are known as bytes.
So why do we need all of this? A computer is a Binary machine deep inside. It knows only ones and zeroes. It uses the Binary System to do everything it does. When your computer shows you Decimal numbers, they are merely a translation of the Binary representation of those numbers inside the machine. Bits and Bytes in Binary form are obviously long and cumbersome, you would not want to work with them on a daily basis.
Since the Byte is the unit of information that our computers work with (A group of 8 bits), it was convenient to find a way to express bytes that was still sort of meaningful to Humans, and easy to work with for a computer. This is where hexadecimal came in to play: One Hex digit can be expressed in four Bits, 8 bits, or two nibbles, or one byte can thus be expressed by two hex digits. The table below will let you convert values easily between the various numbering systems:
As said before, a Byte, the natural unit of information for a computer, is 8 bits wide. Therefore, the largest number a computer can easily handle will be 11111111B. It is fairly easy to convert this number to a Decimal number, after all we know it means in Binary:
All we have to do to convert the numbers in the right-hand side mathematical expression to Decimal, according to the table listed above:
That is one of the magic numbers you are probably familiar with: 255 is the largest Decimal number a computer can store in one byte. The same holds true for Hexa-decimal:
With the little table above, it would have been simple to just split the byte in two nibbles, and then to look up the Hex digit for each nibble; 1111B = FH, so 1111 1111B = FFH. You will find that little table works the other way around too: E7H = 1110 0111B because EH = 1110B and 7H = 0111B. For conversions to and from Decimal, you still have to do the Math though. Or, you can reference the more complete table below.
You should now be able to look at numbers in either one of the three systems, Hexadecimal, Binary or Decimal, and understand how to convert them to each other. The easiest way to do so is use of the table above. This will matter later when the details of the IP networking protocol are discussed in great detail. If you do not get the Math yet, read this again in a day or two. Once you grasp the concept, it is not hard. Although most of the math will actually be done in the examples, you will gain better understanding if you do not just assume what is said, but actually understand what is happening and why.