The Decimal System
It may not have occurred to you, but in everyday life you use a system based on digits to represent numbers. In this case, we use decimal digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) and refer to the system as the decimal system.
Consider what the number 83 means. It means eight tens plus three:
83=8*10+3
The number 4,728 means four thousands, seven hundreds, two tens, plus eight:
4728=4*1000 + 7*100 + 2*10 + 8
The decimal system is said to have a base, or radix, of ten. This means that each digit in the number is multiplied by ten raised to a power corresponding to that digit's position. Thus,
83 = 8 * 101 + 3 * 100
4728=4*103 + 7*102 + 2*101 + 8*100
Fractional values are represented in the same fashion. Thus,
472.83=4*102+ 7*101+2*100+8*10-1 +3*10-2
In general, for
Decimal representation of X ={... x2x1x0 . x-1x-2x-3...}
the value of X is
X = S xi10i
The Binary System
In the decimal system, ten different digits are used to represent numbers with a base of ten. In the binary system, we have only two digits, 1 and 0. Thus numbers in the binary system are represented to the base two.
To avoid confusion, we will sometimes put a subscript on a number to indicate its base. For example 83 and 4728 are numbers represented in decimal notation, or more briefly, decimal numbers. Now, 1 and 0 in binary notation have the same meaning as in decimal notation:
02 = 010
12=110
How do we represent larger numbers? As with decimal notation, each digit in a binary number has a value depending on its position:
102 = 1 * 21 + 0 * 20 = 210
112 = 1 * 21 + 1 * 20 = 310
100 = 1 * 22 + 0 * 21 + 0 * 20 = 410
and so on.
Again, fractional values are represented with negative powers of the radix:
1001.101 = 23 + 20 + 2-1 + 2-3
Converting Between Binary and Decimal
It is a simple matter to convert a number from binary notation to decimal notation. In fact, we showed several examples in the previous subsection. All that is required is to multiply each binary digit by the appropriate power of 2 and add the results.
To convert from decimal to binary, the integer and fractional parts are handled separately. Suppose it is required to convert a decimal integer N into binary form. If we divide N by 2, in the decimal system, and obtain a quotient N1 and a remainder r1, we may write
N = 2* N1 + r1 r1 = 0 or 1
Next we divide the quotient N1 by 2. Assume that the new quotient is N2 and the new remainder r2. Then
N1 = 2 * N2 + r2 r2 = 0 or 1
so that
N = 2(2N2 + r2) + r1 = 22N2 + r2 * 21 + r1 * 20
If next
N2 - 2N3 + r3
we have
N = 23N3 + r3 * 22 + r2 * 21 + r1* 20
Continuing thus, since N> N1>N2...., we must eventually obtain a quotient Nk = 1 and a remainder rk which is 0 or 1. Then
N = 1 * 2k + rk * 2k-1 + ... + r3* 22 + r2* 21 + r1* 20
That is, we convert from base 10 to base 2 by repeated divisions by 2. The remainders and the final quotient, 1, give us, in order of increasing significance, the binary digits of N.
The fractional part involves repeated multiplication by 2. At each step, the fractional part of the decimal number is multiplied by 2. The digit to the left of the decimal point in the product will be 0 or 1 and contributes to the binary representation, starting with the most significant bit. The fractional part of the product is used as the multiplicand in the next step. To see that this works, let us take a positive decimal fraction F < 1. We can express F as
F=a-1*1/2+a-2*1/22+a-3*1/23+ ...
where each a-i is 0 or 1. If we multiply this by 2, we obtain
2F = a-1 + a-2*1/2 + a-3*1/22 + ...
The integer parts of these two expressions must be equal. Hence the integer part 2F, which must be either 0 or 1 since 0 < F < 1, is simply a-1. Thus 2F = a-1+ F1where 0 < F1< 1 and where
F1 = a-2* 1/2 + a-3*1/22 + ...
To find a-2we now repeat the process. This process is not necessarily exact; that is, a decimal fraction with a finite number of digits may require a binary fraction with an infinite number of digits. In such cases, the conversion algorithm is usually halted after a prespecified number of steps, depending on the desired accuracy.
Hexadecimal Notation
Because of the inherent binary nature of digital computer components, all forms of data within computers are represented by various binary codes. We have seen examples of binary codes for text and binary notation for integers. Later, we shall see examples of the use of binary codes for other types of data. However, no matter how convenient the binary system is for computers, it is exceedingly cumbersome for human beings. Consequently, most computer professionals who must spend time working with the actual raw data in the computer prefer a more compact notation.
What notation to use? One possibility is the decimal notation. We have seen that every binary number has a decimal equivalent. This is certainly more compact than binary notation, but it is awkward because of the tediousness of converting between base 2 and base 10.
Instead, a notation known as hexadecimal has been adopted. Binary digits are grouped into sets of 4. Each possible combination of 4 binary digits is given a symbol, as follows:
0000 = 0 1000 = 8
0001 = 1 1001 = 9
0010 = 2 1010 = A
0011 = 3 1011 = B
0100 = 4 1100 = C
0101 = 5 1101 = D
0110 = 6 1110 = E
0111 = 7 1111 = F
Since 16 symbols are used, the notation is called hexadecimal, and the 16 symbols are the hexadecimal digits.
A sequence of hexadecimal digits can be thought of as representing an integer in base 16. Thus,
1A16 = 116 * 161 + A16* 160 = l10*161+ l010*160=26
But hexadecimal notation is not used just for representing integers. It is used as a concise notation for representing any sequence of binary digits, whether they represent text, numbers, or some other type of data. The reasons for using hexadecimal notation are
1. It is more compact than binary notation.
2. In most computers, binary data occupy some multiple of 4 bits, and hence some multiple of a single hexadecimal digit.
3. It is extremely easy to convert between binary and hexadecimal.
As an example of the last point, consider the binary string 110111100001. This is equivalent to
1101 1110 0001 = DE116
D E 1
This process is performed so naturally that an experienced programmer can mentally convert visual representations of binary data to their hexadecimal equivalent without written effort. It is quite possible that you will never need this particular skill. Nevertheless, since you may encounter hexadecimal notation, this discussion has been included in the text.