1.8 Review of Number Systems

Throughout this book we use the decimal (base 10), binary (base 2), octal (base 8), and hexadecimal (base 16) number systems. A concise review of number systems and representations for integer data will conclude this introductory chapter. You should skip this material only if you are already adept with conversions among these representations, including expressions of negative integer values.

All data stored and manipulated in contemporary computers exist in binary form. Integers, floating-point numbers, characters, and instructions exist as sequences of zeros and ones. This binary representation is base 2.

When we display binary data, we usually use base 8 (octal), base 10 (decimal), or base 16 (hexadecimal), instead of base 2 (pure binary). These larger bases are easier for humans to comprehend than long strings of binary digits, but the value of a stored chunk of numeric information is the same, regardless of the base used to represent it.

Different bases are suitable for different applications. Bases 8 and 16 are particularly useful for emphasizing patterns of bits within a stored unit, while base 10 is useful for understanding the everyday value of a stored number, since it is the base used for counting in natural human languages.

1.8.1 Positional Coefficients and Weights

The most frequently encountered numbering system involves positional coefficients and weights. The digit value at each position in a number is its positional coefficient. The weight of each digit is a successively larger power of the base of the number system, from right to left. We can express a value Q in the number system with base r (also called the radix), as follows, allowing for a fractional portion {in braces}:

Q = xnwn + xn 1w n 1 + ... + x1w1 + x0w0 + {x 1w 1 + ... + x mw m}

where

wi = ri

that is, weight = r i and r = radix or base, and

0 xi r 1

that is, xi = positional coefficient.

This formalism ensures that the largest value of a positional coefficient is always one less than the base value i.e., 1 for base 2 (binary), 9 for base 10 (decimal), and so forth. When the base is greater than 10, the letters A, B, … can be used to convey the positional coefficients with values of 10 and beyond. Base 16 (hexadecimal) uses the letters A through F to represent numerical values 10 through 15, respectively.

We will illustrate these concepts by expressing the value 154 in the bases 2, 8, 10, and 16, as follows:

154

=

1 x 102 + 5 x 101 + 4 x 100

(base 10)

 

=

1 x 27 + 0 x 26 + 0 x 25 +1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20

(base 2)

 

=

2 x 82 + 3 x 81 + 2 x 80

(base 8)

 

=

9 x 161 + A x 160

(base 16)

That is, the value 154 results from summing the non-zero terms:

154

=

100 + 50 + 4

(base 10)

 

=

128 + 16 + 8 + 2

(base 2)

 

=

128 + 24 + 2

(base 8)

 

=

144 + 10

(base 16)

In summary: 15410 = 100110102 = 2328 = 9A16.

Up to this point, we have used a subscript of 16 when denoting representations of base 16. Notations commonly used in computer science are 0X or 0x (e.g., compilers for the C language) or a suffix of H or h (e.g., some Intel documentation) when expressing a hexadecimal value: 15410 = 9A16 = 0x9A = 9AH.

1.8.2 Binary and Hexadecimal Representations

Modern digital computers use the binary number system internally because the most practical physical components are intrinsically binary in nature. Since long strings of 0s and 1s are cumbersome for human beings, most computer professionals routinely use base 16, or occasionally base 8. The various system software components assemblers, compilers, linkers, and so forth readily convert such numbers to their binary equivalents. Table 1-6 shows the binary, octal, and hexadecimal equivalents for the decimal values 0 through 16.

Base 8 and 16 representations are not only convenient but are easily derived from binary representations. Converting from binary to base 8 or 16 simply requires separating the binary number into 3-bit (for octal) or 4-bit (for hexadecimal) groups, from right to left, and then replacing each binary group with the appropriate digit for the new base (from Table 1-6). Consider the following illustration of decimal value 1249:

0100111000012

  

(base 2)

010 011 100 0012

=

23418

(base 8)

0100 1110 00012

=

4E116

(base 16)

Although this process is relatively intuitive, you may already appreciate that some pocket calculators have the capability to convert numbers amongst these common bases.

Table 1-6. Conversion Table for the First Few Integers

Decimal

Binary

Octal

Hexadecimal

0

0000

0

0

1

0001

1

1

2

0010

2

2

3

0011

3

3

4

0100

4

4

5

0101

5

5

6

0110

6

6

7

0111

7

7

8

1000

10

8

9

1001

11

9

10

1010

12

A

11

1011

13

B

12

1100

14

C

13

1101

15

D

14

1110

16

E

15

1111

17

F

16

1 0000

20

10

The system software components for the assembly language programmer may express their outputs program listings, linker maps, debugging aids, and dumps of memory contents in octal or hexadecimal by default; decimal conversion may also be offered. Sometimes manual interpretation is required, as when several data elements of different bit widths have been packed for storage as one composite binary number.

1.8.3 Signed Integers

In the history of computer development, three methods have been considered for binary representations of ranges that include negative as well as positive integers. These methods are: sign and magnitude, one's complement, and two's complement. Sign and magnitude requires greater complexity at the physical implementation level, while one's complement introduces the complication of having two bit patterns that both represent the number zero. Since testing for zero is a common operation, the need to consider two cases would require greater complexity at the physical implementation level. Accordingly, contemporary computers use two's complement representation for signed integers.

All methods for representing signed integers within N bits have the net effect of allocating one bit to represent positive/negative and the remaining N 1 bits to represent the number's magnitude. For two's complement, zero is considered a positive number. Two's complement representation offers the advantage of making successive additions or subtractions of one work smoothly right through zero. Table 1-7 shows how small signed numbers can be represented by three bits. All of the representations agree for positive values, but differ in the handling of zero and negative values.

Table 1-7. Representations for Small Integer Values

Value

Two's Complement

One's Complement

Sign and Magnitude

+3

011

011

011

+2

010

010

010

+1

001

001

001

0

000

000 and 111

000 and 100

1

111

110

101

2

110

101

110

3

101

100

111

4

100

too big to represent

too big to represent

Notice that the two's complement representation looks like a complete binary counting sequence (0 to 7) that has been cut in half (0 to 3, and 4 to 7) and restacked. Also appreciate that the range of integers that can be represented within N bits extends from the value 2 N 1 through 0 to +2 N 1 1.

No special action is required to form the two's complement representation for a positive integer, except to realize that the most significant bit must be an explicit zero. Forming the two's complement of a negative integer can be accomplished by subtracting the magnitude from zero, for example:

0

000

 

-(+3)

-(011)

(subtraction, with due attention to "borrowings")

-3

101

 

Another method is to perform a bit-by-bit complementation of the positive number (including its zero sign bit) and then to add 1 to that intermediate result:

+3

011

becomes

100

(intermediate result)

   

+(001)

(addition, with due attention to "carries")

3

  

101

 

In either of these methods, any borrowing or carrying outside of the N-bit field is not to be written as part of the final result.

Finding the hexadecimal representation of a negative integer proceeds similarly by mentally subtracting each digit from "F" and then adding 1 to that intermediate result:

+15502

3C8E

becomes

C371

(intermediate result)

   

+(0001)

(addition, with due consideration for "carries")

15502

  

C372

 

Fortunately, as with all such techniques, familiarity comes with practice.

You will find that an understanding of hexadecimal and binary number systems is helpful to your study of architecture and assembly language programming, as well as other computer concepts.



ItaniumR Architecture for Programmers. Understanding 64-Bit Processors and EPIC Principles
ItaniumR Architecture for Programmers. Understanding 64-Bit Processors and EPIC Principles
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net