Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
Скачиваний:
475
Добавлен:
12.08.2013
Размер:
4.99 Mб
Скачать

44

Chapter 3

However, the machine calculation of the two’s complement of the same value often produces a different result, for example:

100000000B

=

28 = 256

decimal

- 00010110B

=

22

decimal

__________

 

___________

11101010B

 

234

decimal

The difference is due to the fact that in the longhand method we have used the next higher integer power of the base compared to the value of the subtrahend (in this case 100000B) while the machine calculations use the next higher integer power of the base compared to the operand’s word size, which is normally either 8 or 16 bits. In this example the operand’s word size is eight bits and the next highest integer power of 2 is 100000000B. In either case, the results from two’s complement subtraction are valid as long as the minuend is an integer power of the base that is larger than the subtrahend.

For example, to perform the binary subtraction of 00011111B (31 decimal) minus

00010110B (22 decimal) we can find the two’s complement of the subtrahend and add, discarding any overflow digit, as follows:

00011111B = 31 decimal

+11101010B = 234 decimal (two’s complement of 22)

_________

100001001B

discard____|

00001001B = 9 decimal (31 minus 22 = 9)

In addition to the simplification of subtraction, two’s complement arithmetic has the advantage that there is no representation for negative 0. It can be argued that there are cases in which a negative zero notation could be useful, but in fact this is usually unnecessary. While both the two’s complement and the one’s complement schemes can be used to implement binary arithmetic, system designers usually prefer the two’s complement.

3.3 Encoding of Fractional Numbers

In any positional number system the weight of each integer digit is determined by the formula:

P = d * BC

where d is the digit, B is the base or radix, and C is the zero-based column number, starting from right to left. Therefore, the value of a multi-digit positive integer to n digits can be expressed as a sum of the digit values:

dn*Bn + dn-1*Bn-1 + dn-2*Bn-2 + ... + d0*B0

where d is the value of the digit and B is the base or radix of the number system. This representation can be extended to represent fractional values. Recalling that we can

Data Types and Data Storage

45

extend the sequence to the right of the radix point, as follows:

 

 

 

 

 

 

 

 

 

 

 

 

 

xn

=

1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

xn

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

INTEGER PART

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

27

= 128

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

26

= 64

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

25

= 32

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

24

= 16

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

23

= 8

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

22

= 4

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

21

= 2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

20

= 1

 

 

 

radix point

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1

1

1

1

1

1

1

1

 

 

 

1

1

1

1

1

1

 

1

1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

FRACTIONAL PART

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.500

 

 

 

 

1/2

 

 

2-1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.250

 

 

 

 

1/4

 

 

2-2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.125

 

 

 

 

1/8

 

 

2-3

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.0625

 

 

 

1/16

 

 

2-4

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.03125

 

 

1/32

 

 

2-5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.015625

 

1/64

 

 

2-6

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.0078125

1/128

 

2-7

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.00390625 1/256 2-8

Figure 3-5 Positional Weights in a Binary Fraction

In the decimal system the value of each digit to the right of the decimal point is calculated as 1/10, 1/100, 1/1000, and so on. The value of each successive digit of a binary fraction is the reciprocal of a power of 2; therefore, the sequence is: 1/2, 1/4, 1/8, 1/16, .... Figure 3-5 shows the positional weight of the integer and fractional digits in a binary number.

In Chapter 2 we used the positional weights of the binary digits to convert a binary number to its decimal equivalent. A similar method can be used to convert the fractional part of a binary number. Using the decimal equivalents shown in Figure 3-5 we convert the binary fraction .10101 to a decimal fraction as follows

 

.1

0

1

0

1

 

|

 

|

 

|

.500

_____________________|

 

|

 

|

.125

_________________________|

 

|

.03125

____________________________|

------

 

 

 

 

 

.65625

 

 

 

 

 

3.3.1 Fixed-Point Representations

The encoding and storage of fractional numbers (also called real numbers) in binary form presents several difficulties. The first one is related to the representation of the radix point. Since there are only two symbols in the binary set, and both are used to represent the numerical value of the number, there is no other symbol available for the decimal point.

46

Chapter 3

binary

decimal

= 00111010 00100000 = 58.125

implied binary point

Figure 3-6 Binary Fixed-Point Representation

One possible solution is to predefine the digit field that represents the integer part and the one that represents the fractional part. For example, if a real number is to be encoded in two data bytes we can assign the high-order byte to encode the integer part and the low-order byte for the fractional part. In this case, the positive decimal number 58.125 could be encoded as shown in Figure 3-6.

In Figure 3-6 we assumed that the binary point is positioned between the eighth and the ninth digit of the encoding. Fixed-point representations assume that whatever distribution of digits is selected for the integer and the fractional part of the representation is maintained in all cases. This is the greatest limitation of the fixed-point formats.

Suppose we want to store the value 312.250. This number is represented in binary as follows:

312 = 100111000

.250 = .01

In this case, the total number of binary digits required for the binary encoding is 11. The number can be physically stored in a 16-digit structure (as the one in Figure

3-6) leaving five cells to spare. However, since the fixed-point format we have adopted assigns eight cells to the integer part of the number, 312.250 cannot be encoded because the integer part requires nine binary digits. In spite of this limitation, the-fixed point format was the only one used in early computers.

3.3.2 Floating-Point Representations

An alternative to fixed-point is not to assume that the radix point has a fixed position in the encoding, but to allow it to float, hence the name floating-point. The idea of separately encoding the position of the radix point originated in scientific notation, where a number is written as a base greater than or equal to 1 and smaller than 10, multiplied by a power of 10. For example, the value 310.25 in scientific notation is written:

3.1025 × 102

A number in scientific notation has a real part and an exponent part. Using the terminology of logarithms these two parts are sometimes called the mantissa and the characteristic. The following simplification of scientific notation is often used in computer work:

3.1025 E2

Data Types and Data Storage

47

In the computer version of scientific notation the multiplication symbol and the base are implied. The letter E, which is used to signal the start of the exponent part of the representations, accounts for the name “exponential form.” Numbers smaller than 1 can be represented in scientific notation or in exponential form by using negative powers. For example, the number .0004256 can be written:

4.256 × 104

or as

4.256 E-4

Floating-point representations provide a more efficient use of the machine’s storage space. For example, the numerical range of the fixed point encoding shown in

Figure 3-6 is from 255.99609375 to 0.00390625. To improve this range we can re-assign the sixteen bits of storage so that four bits are used for encoding the exponent and twelve bits for the fractional part, called the significand. In this case the encoded number appears as follows:

0000 000000000000

+--+ +----------

+

|__________ 12-bit fractional part

(significand)

|___________________ 4-bit exponent part

If we were to use the first bit of the exponent to indicate the sign of the exponent, then the range of the remaining three digits would be 0 to 7. Note that the sign of the exponent indicates the direction in which the decimal point is to be moved; this is unrelated to the sign of the number. In this example, the fractional part (or significand) could hold values in the range 1,048,575 to 1. The combined range of exponent and significand allows representing decimal numbers in the range 4095 to 0.00000001 that considerably exceeds the range in the same storage space in fixed-point format.

3.3.3 Standardized Floating-Point Representations

Both the significand and the exponent of a floating-point number can be stored as an integer, in sign-magnitude, or in radix complement form. The number of bits assigned to each field varies according to the range and the precision required. For example, the computers of the CDC 6000, 7000, and CYBER series used a 96-digit significand with an 11-digit exponent, while the PDP 11 series used 55-digit significands and 8-digit exponents in their extended precision formats.

Variations, incompatibilities, and inconsistencies in floating-point formats led to the development of a standard format. In March and July 1985, the Computer Society of the Institute of Electric and Electronic Engineers (IEEE) and the American National Standards Institute (ANSI) approved a standard for binary floating-point arithmetic (ANSI/IEEE Standard 754-1985). This standard establishes four formats for encoding binary floating-point numbers. Table 3.1 summarizes the characteristics of these formats.

48

 

 

 

Chapter 3

 

 

Table 3.1

 

 

 

ANSI/IEEE Floating Point Formats

 

 

 

 

 

 

PARAMETER

SINGLE

SINGLE

DOUBLE

DOUBLE

 

 

EXTENDED

 

EXTENDED

 

 

 

 

 

total bits

32

43

64

79

significand bits

24

32

53

64

maximum exponent

+127

1023

1023

16383

minimum exponent

-126

1022

-1022

16382

exponent width

8

11

11

15

exponent bias

+127

---

+1023

---

3.3.4 IEEE 754 Single Format

Figure 3-7 shows the IEEE floating-point single format.

31

 

30

 

 

 

 

 

 

 

22

 

 

 

 

 

 

0

 

 

bits

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

exponent (8 bits)

 

 

 

 

 

 

 

 

mantissa (23 bits)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

sign of the number (1 bit)

Figure 3-7 IEEE Floating-Point Single Format

If a floating-point encoding is to allow the representation of signed numbers it must devote one binary digit to encode the number’s sign. In the IEEE 754 single format in Figure 3-7 the high-order bit represents the sign of the number. A value of 1 indicates a negative number.

The exponent of a binary floating-point number represents the integer power of the base with which the significand must be multiplied. The exponent can be stored in integer, sign magnitude, or radix complement representations. The IEEE 754 standard for floating-point arithmetic establishes that the exponent be stored in biased form, although the bias is not defined in all formats defined in the standard.

The word bias, in this context, means a constant that is added to the exponent in order to determine its final value. The term excess-n notation has also been used in this context. The constant is usually calculated to be approximately one-half the numerical range of the exponent field. For example, the IEEE single format devotes eight digits for the exponent field (see Figure 3-7). The numerical range of eight binary digits is 0 to 255 decimal and one-half of this range is approximately 127. Adding the constant 127 to all positive exponents places them in the range 127 to 255. The lower half of the range (1 to 126) is used for negative exponents. A 0-value in the exponent field is reserved to encode zero and denormals. Denormals are a special type of number discussed in the following paragraph. Table 3.2 shows the values of the exponent and the biased representation in the IEEE single format for floating-point numbers.

Data Types and Data Storage

 

 

49

 

 

Table 3.2

 

 

 

Interpretation of Exponent in the IEEE Single Format

 

 

 

 

 

 

BIASED

SIGN OF

TRUE

 

 

 

EXPONENT

NUMBER

EXPONENT

SIGNIFICAND

CLASS

 

 

 

 

 

 

0000 0000

+

-

00 ... 00

positive zero

 

-

-

00 ... 00

negative zero

 

 

 

11 ... 11

 

 

 

 

 

to

 

 

 

 

 

00 ... 01

denormals

 

0000 0001

-

-126

00 ... 00

normals

to

 

to

to

 

 

0111 1111

 

0

11 ... 11

 

 

1000 0000

-

1

00 ... 00

normals

to

 

to

to

 

 

1111 1110

 

127

11 ... 11

 

 

1111 1111

+

-

00 ... 00

+ infinity

 

-

 

00 ... 00

- infinity

 

-

 

10 ... 00

indefinite

 

-

 

00 ... 01

 

 

 

 

 

to

 

 

 

 

 

11 ... 11

not-a-number

 

Note in Table 3.2 that the exponent value 00000000B is used to represent zero and denormal numbers. Denormals, or denormalized numbers, occur when the exponent of the number is too small to represent in the corresponding floating-point format. On the other hand, the exponent 11111111B is used to encode numbers that are too large for the single format, or to represent error conditions. The exponent range 00000001B to 11111110B (decimal values 1 to 254) is used to represent normal numbers, that is, numbers that are within the range of the format.

In IEEE 754 floating-point formats the high bit of the exponent field does not encode the sign, as is the case in the sign-magnitude form. Instead, the bias 127 scheme, mentioned previously, is used to represent negative and positive exponents. Negative exponents are in the range 1 to 127 (see Table 3.2) and positive exponents are in the range 128 to 254. In contrast with fixed point conventions, the high bit of the exponent is set to indicate a positive exponent, and is zero to indicate negative exponent. The main advantage of a biased exponent is that the numbers can be compared bitwise, from left to right, to determine the larger one. The number’s true exponent is obtained by subtracting the bias.

The third field of the floating-point representation is known by several names: fractional part, mantissa, characteristic, and significand (see Figure 3-7). The word significand is the one most commonly used in the literature. Like the exponent, the significand can be stored as an integer, or in sign-magnitude or radix complement representations.

A floating-point binary number is said to be in normalized form when the first digit of its significand is 1. An un-normalized binary floating-point number can be normalized by successively shifting the digits of the significand to the left, while simultaneously subtracting one from the exponent. This process is continued until the

50

Chapter 3

high-order bit of the significand is a binary 1. The process does not change the value of the number, since shifting the significand bits to the left effectively multiplies the number by 2, while subtracting one from the exponent divides the number by 2.

Clearly, the value of a number does not change if it is multiplied and divided by the same value. Also, note that normalization applies to the entire encoded number since it requires adjustments of both the exponent and the significand. Therefore, it is not correct to speak of a normalized significand or a normalized mantissa; we should refer to the significand of a normalized floating-point number.

One advantage of the normalized form is that the significand contains a maximum number of significant bits. However, addition and subtraction of floating-point numbers require that both operands have the same exponent. Therefore, before performing these operations it is often necessary to shift the significand digits to the right or to the left so that the exponents are equal.

The IEEE standard takes advantage of the fact that a normalized significand of a binary floating-point starts with a 1-digit. In the singleand double-precision formats this leading bit of the significand is assumed, in effect doubling the range of the representation. Not so in the extended formats, in which the digit must be explicitly coded. Note that this assumption is not valid if the exponent is all zeros. A zero exponent and a non-zero significand indicate a denormal, as shown in Table 3.2. Also, the use of an implicit bit makes necessary a special representation for zero (see Table 3.2). This special zero must be handled separately during arithmetic operations.

3.3.5 Encoding and Decoding Floating-Point Numbers

The formats in the IEEE 754 standard for binary floating-point arithmetic were designed to provide maximum storage capacity and processing efficiency. For example, the exponent in the IEEE single format, stored in biased form, takes up eight bits; however, these eight bits do not fall on a byte boundary. The exponent bits take up seven bit positions in the high-order byte, and one bit position in the next byte, as shown in Figure 3-7. In the same IEEE single encoding the significand takes up seven bits of the second byte as well as the third and fourth bytes. The sign of the number is the high-order bit of the high-order byte. Figure 3-8 shows the number 127.375 stored in the IEEE floating-point single format.

The encoding in Figure 3-8 is interpreted as follows:

sign of number = 0 (positive)

biased exponent = 10000101B = 133 decimal real exponent = 133 - bias = 133 - 127 = 6

significand = 1.1111110 11000000 00000000 (adding explicit digit) significand is adjusted by moving the radix point six places

to the right

new significand = 1111111.01100...000

The significand bits are intepreted as follows::

integer part = 1111111 = 127

fractional part = .01100..00 = .375