CHAPTER 6
MACHINE REPRESENTATION OF DATA
ON DIFFERENT BASES

The most frequently used number systems are: binary, octal, decimal, and hexadecimal. The binary number system uses the numbers {0,1}, the octal system {0-7}, the decimal number system {0-9}, and the hexadecimal (HEX) number system {0-9, A,B,C,D,E,F}. Those number systems are said to be the base of the numbers. Table A.1 denotes such a representation. What follows are conversion procedures to represent numbers in different bases.

A.1 TABLE OF NUMBERS

DecimalOctalHexadecimalBinary
0000000
1110001
2220010
3330011
4440100
5550101
6660110
7770111
81081000
91191001
1012A1010
1113B1011
1214C1100
1315D1101
1416E1110
1517F1111

DecimalOctalHexadecimalBinary
0000000
1110001
2220010
3330011
4440100
5550101
6660110
7770111
81081000
91191001
1012A1010
1113B1011
1214C1100
1315D1101
1416E1110
1517F1111

A.2 REPRESENTATION OF NUMBERS FROM BASE 10 TO ANOTHER BASE

The technique to convert an integer number from base 10 to another base is:

Divide the base 10 number by the new base number, i.e., divide by 2 when converting to base 2, divide by 8 when converting to base 8, etc.

The remainder of the division in step 1 is the digit of the number in the new base. This digit is located at the left of the decimal point in the number of the new base.

If the result of the division is bigger or equal than the new base number, repeat step 1. Else, the resultant number is located to the left of the previous digit, i.e., as the most significant digit.

Example

a) Convert the number 105 from base 10 to its corresponding number in base 8

105(10) to ?(8)

remainder

8 105 1 105 / 8 : result = 13, remainder = 1

8 13 5 13 / 8 : result = 1, remainder = 5

1 1 result 8, stop.

Answer: 105(10) to 151(8)

b) convert the same number from base 10 to base 2.

105(10) to ?(2)
remainder
2105 1
2 52 0
2 26 0
2 13 1
2 6 0
2 3 1
1 1

Answer: 105(10) to 1101001(2)

c) Similarly, the conversion to base 16 could be done as:

105(10) to ?(2)

U>A
remainder
16105 9
6 6

nswer: 105(10) to 69(16)

To represent a real number given in base 10 to another base , the previous procedure for the integer part shall apply to the number to the left-hand side of the decimal point. The following procedure is followed for the fractional part.

Obtain the fractional part and multiply it by the new base number.

The integer part of the resultant product in step 1 is the digit of the fractional part of the number in the new base. It is located to the right side of the decimal point and of the previous digits if any.

If the fractional part of the product in step 1 is equal to zero, stop. Else, the fractional part of the multiplication is used to repeat step 1 until the desired precision is reached.

Example

a) Convert 0.15(10) to base 16.

0.15(10) = ?(16)

U>A
integer
0.15 x 16
2.40 x 16
6.40 x 16
6.40 x 16
6.40

nswer: 0.15(10) = 0.266(16)

b) Convert 0.120(10) to base 8.

0.120(10) = ?(8)

integer
.120 x 8
0.960 x 8
7.680 x 8
5.440 x 8
3.520 x 8
4.160 x 8
1 .280 x 8
2.240

Answer: 0.120(10) = 0.0753412(8)

A.3 A GENERAL REPRESENTATION OF NUMBERS IN DIFFERENT BASES

The procedure to obtain the decimal representation of any number in another base obeys the following expression:

Decimal value =

where b is the base number, bi is the positional weight, and di is the positional value. The left digit next to the decimal point has the position i = 0, the value of i increases to the le ft digits, and decreases to the right digits.

Example

1101101(2) =

= 64 + 32 + 8 + 4 + 1 = 109(10)

110.11(2) =

= 4 + 2 + 0.5 + 0.25 = 6.75(10)

151(8) = = 54 + 40 + 1 = 105(10)

Table 3.2 Counting from 0 to 15 in Decimal and Binary

Decimal Binary Decimal Binary
00 0000 08 1000
010001091001
020010101010
030011111011
04 0100 12 1100
050101131101
060110141110
070111151111

3.3 NUMBER SYSTEMS AND ARITHMETIC

As noted earlier, computers can perform arithmetic and logical operations on suitably coded data. Based on our discussions of logic devices, we have some idea of what types of logical operations can be performed and how digital devices can be con structed to perform such operations. Example 3.5 also gave us an indication of how logic devices can perform arithmetic operations. Since computers are based on bistable devices, the natural number system to be employed is binary (base 2) rather than the usual decimal (base 10). First we will discuss the binary number system and binary arithmetic, then briefly present other number systems (hexadecimal and octal) that are also useful in working with computer systems.

Let us briefly review how the decimal, or base 10, system works. The sequence of decimal numbers has a very specific meaning, for example,

204 =

412.05 =

Notice that we have positions representing powers of 10 and carrying digit position weights (e.g., 100, 101) and the digits 0,1,2 . . . 9. We multiply the symbol in a particular position by its digit p osition weight and sum to get the decimal number. In counting or adding we use the 10 digits (0 . . . 9), and when we exceed nine we carry a one to the next position.

In the binary number system there are two digits, 0 and 1, corresponding to the two possible states of the digital signals (e.g., HI and LO). A binary number is, therefore, a sequence of 1's and 0's. Binary numbers can also be represented by multipl ying the digit in each position by their digit position weights (powers of 2 in this case), for example,

01001101(2) =

where "(2)" has been used to indicate that this is a binary number. Because 0's and 1's are the digits used in the binary system, they are often referred to as bits, an acronym for binary digits. The leftmost 1 in a binary number is called the most significant bit (MSB), the extreme right digit is called the least significant bit (LSB).

The conversion from binary to decimal numbers is readily accomplished by multiplying each symbol by its digit position weight and summing, for example,

01001101(2) =

= 77(10)

Two methods can be used for conversion from decimal to binary:

Divide the decimal number by 2 (the binary base) and record the remainder as the LSB of the binary equivalent. Then divide the quotient from the first division of two and repeat until the quotient has been reduced to zero. For example, to find the binary equivalent of 29(10):
QuotientRemainder
29/2 =141(LSB)
14/2 =70
7/2 =31
3/2 =01
1/2 =01(MSB)
Thus, 29(10) = 111 01(2)

Subtract the highest possible power of two (the binary base) from the decimal number and place a 1 in the appropriate weighting position, repeat until the decimal number is reduced to zero. If after the first subtraction the next lower power of 2 cannot be subtracted, then place a 0 in the appropriate weighting position. Let us again find the binary equivalent of 29(10):
29 - 24 = 29 - 16= 131 (MSB)
13 - 23 = 13 - 8= 51
5 - 22 = 5 - 4= 11
1 - 21 = (cannot be done) 0
1 - 20 = 1 - 1= 01 (LSB)
This confirms our previous result: 29(10) = 11101(2).

Binary addition is performed very much like decimal addition. We add two binary symbols and, if the largest symbol is exceeded, we carry a one to the next position or place, for example,

and,

So far we have avoided negative numbers and subtraction. There are two common ways to handle these in binary arithmetic:

Direct binary subtraction is like normal decimal subtraction except when we need to borrow we use a 2 (t he binary base), for example,

We must always subtract the smaller number from the larger number and then adjust the sign, for example,

This is difficult to implement on a computer, and the second approach, which follows, is preferred.

Two's complement arithmetic is used on computers for negative numbers and subtraction. The two's complement of a binary number is that number which when added to the original number results in a sum of zero, for example,

Binary number: 110110110110(2)

Two's complement: 001001001010(2)

Carry bit and sum: 1 000000000000(2)

Notice that the carry bit (1) in the resulting sum is ignored. Thus we work with a fixed number of bits (12 in this example). This is similar to the odometer of an automobile. For example, if the odometer has five digits and reads 99995(10) m iles, after 5 more miles the reading becomes zero. To obtain the two's complement of a binary number, we first calculate the one's complement by setting each bit to the opposite value, then and one to the one's complement, for example,

Binary number: 110(2)

One's complement: 001(2)

Two's complement: 010(2)

Thus, with a fixed number of binary digits the two's complement notation is used two represent negative numbers. For example, with an 8-bit binary number we can represent 28 = 256 possible values (00000000(2) to 11111111(2)). Using the two's compleme nt notation, we can represent numbers in the range +127(10) to -128(10) (see Fig 3.10). The leftmost bit serves as a sign bit, where zero implies a positive number and 1 implies a negative number. Note that 0(10) = 000000000(2) is considered to be a pos itive number.

Binary numbers are natural for computers, but very inconvenient for people. It is too easy to make a mistake, and long sequences of binary digits are difficult to interpret. Other number systems, notably hexadecimal (base 16) and octal (base 8), are commonly used by computer programmers. These are sometimes referred to as grouped bit number systems, since groups of four bits are easily converted two hexadecimal, and groups of three bits are easily converted to octal. Most microprocessors today use 8 or 16 bits, making groupings of 4 bits more convenient. Thus, the hexadecimal system is generally preferred and will be emphasized here.

Octal numbers use the symbols 0,1,2 . . . 7 and digit position weights that are powers of 8. Similarly, hexadecimal numbers use the symbols 0,1,2 . . . 9 and A, B, C, D, E, and F and digit position weights that are powers of 16. Numbers up to 23(10) and their hexadecimal, octal, and binary equivalents are shown in Table 3.3.

Table 3.3 Decimal Numbers Up to 23 and Their Hexadecimal, Octal, and Binary Equivalents
DecimalHexa- decimal Octal Binary DecimalHexa- decimal Octal Binary
000012C141100
111113D151101
2221014E161110
3331115F171111
4 4 4 100 16 10 20 10000
55510117112110001
66611018122210010
77711119132310011
8 8 10 1000 20 14 24 10100
9911100121152510101
10A12101022162610110
11B13101123172710111
sing this table, we can readily convert binary numbers, in 3-bit groups, to their octal equivalent, for example,

Binary number: 011 010 111 101

Octal equivalent: 3 2 7 5

Thus, 011010111101(2) = 3275(8). Similarly, using 4-bit groupings, we can convert to hexadecimal,

Binary number: 0110 1011 1101

Hexadecimal equivalent: 6 B D

Thus, 011010111101(2) = 6BD(16). The basic methods and concepts described for binary numbers regarding conversion, negative numbers, and arithmetic also apply to octal and hexadecimal numbers. These are illustrated for hexadecimal numbers in the exam ples that follow.

Example 3.6 Conversion between Decimal and Hexadecimal

First we evaluate the decimal equivalent of 19AF(16).

19AF(16) =

=

= 6575(10)

Now we convert the decimal number 29(10) to hexadecimal using the division method.
QuotientRemainder
29/16 =113(10) = D(16)(LSD)
1/16 =0 1(10) = 1(16)(MSD)
hus, 29(10) = 1D(16). Confirm this result by subtracting powers of the base.

Example 3.7 Hexadecimal Addition

Two examples are given here.

and,

Example 3.8 Hexadecimal Subtraction

We can handle negative numbers and subtraction by using the 16's complement of a hexadecimal number. For example, to subtract 86B(16) from A94(16) we add the 16's complement of 86B( 16) to A94(16). The 16's complement of 86B(16) is obtained by getting the 15's complement of 86B(16) then adding one. The 15's complement is obtained by subtracting each digit from 15, for example,

15's complement of 86B(16) = 794(16)

16's complement of 86B(16) = 795(16)

A94(16) 86B(16) = A94(16) + 795(16) = 1229(16)

Neglecting the carry, we get the result 229(16). Check this result by direct hexadecimal subtraction.

3.4 SUMMARY

This chapter has reviewed some background material that will be useful for under-standing the operation and programming of microcomputer systems.

First we introduced the basic logic gates (i.e., AND, OR, XOR, NOT, and EQ) and Boolean algebra. These gates were then used to illustrate some simple digital logic circuits (i.e., flip-flops, registers, and counters).

Finally, we introduced the binary, octal, and hexadecimal number systems and their use in simple arithmetic operations. Conversions between these number systems and the two's complement representation of binary numbers were also considered.


Back to title page Go to previous Chapter Go to next Chapter
This HTML document was created by GT_HTML 6.0d 01/23/97 2:19 PM.