• 沒有找到結果。

Operations on Bits—Part I: Arithmetic

在文檔中 introduction to computing systems (頁 52-56)

2.3 2's Complement Integers

2.5 Operations on Bits—Part I: Arithmetic

Therefore, a i — 0.

13 = a6 • 23 + a5 • 22 + a4 • 21 + a3 - 2°

Therefore, a3 = 1.

6 = a6 • 22 + a5 • 21 + a4 • 2°

Therefore, <34 = 0.

3 = a6 • 21 + a5 • 2°

Therefore, = 1.

1 = a6 -

Therefore, = 1, and we are done. The binary representation is 01101001.

Let's summarize the process. If we are given a decimal integer value N, we construct the 2's complement representation as follows:

1. We first obtain the binary representation of the magnitude of N by forming the equation

N = a6 • 26 + a5 • 25 + <z4 • 24 + a3 • 23 + a2 • 22 + ax * 21 + • 2°

and repeating the following, until the left side of the equation is 0:

a. If N is odd, the rightmost bit is 1. If N is even, the rightmost bit is 0.

b. Subtract 1 or 0 (according to whether N is odd or even) from N, remove the least significant term from the right side, and divide both sides of the equation by 2.

Each iteration produces the value of one coefficient a,-.

2. If the original decimal number is positive, append a leading 0 sign bit, and you are done.

3. If the original decimal number is negative, append a leading 0 and then form the negative of this 2's complement representation, and then you are done.

2.5 Operations on Bits—Part I: Arithmetic

2.5.1 Addition and Subtraction

Arithmetic on 2's complement numbers is very much like the arithmetic on decimal numbers that you have been doing for a long time.

Addition still proceeds from right to left, one digit at a time. At each point, we generate a sum digit and a carry. Instead of generating a carry after 9 (since 9 is the largest decimal digit), we generate a carry after 1 (since 1 is the largest binary digit).

Using our five-bit notation, what is 11 + 3?

The decimal value 11 is represented as 01011 The decimal value 3 is represented 00011 The sum, which is the value 14, is O H I O

S u b t r a c t i o n is s i m p l y a d d i t i o n , p r e c e d e d b y d e t e r m i n i n g t h e n e g a t i v e of t h e n u m b e r t o b e s u b t r a c t e d . T h a t is, A — B i s s i m p l y A + (—B).

The decimal value 14 is represented as 01110 The decimal value 9 is represented as 01001

First we form the negative, that is, -9: 10111

Adding 14 to -9, we get . 01110

which results in the value 5. 00101

Note again that the carry out is ignored.

What happens when wo add a number to itself (e.g., jr + jr)?

Let's assume for this example eight-bit codes, which would allow us to represent integers from - 1 2 8 to 127. Consider a value for A, the integer 59, represented as 00111011. If we add 59 to itself, we get the code 01110110. Note that the bits have all shifted to the left by one position. Is that a curiosity, or will thul happen all tiie time long as the sum a +• a is not too large to represent with the a\;iilahle miinher of hiK.'

Using our positional notation, the number 59 is

0 - 26 + I • 2s -I I • 2'1 + I • 23 -M) • 2* t I • 21 H • 2°

The sum 59 f 59 is 2 • 59, which, in our representation, is

2 • (0 • 2° I I • 2s + 1 • 24 + I • 2* M> • 22 f I • 21 +• I • 2°) But that is nothing more than

0 • 27 + 1 • 26 I 1 - 2 * + 1 - 24 + 0 - 23 + 1 - 22 + I • 21

which shifts each digit one position to the left. Thus, adding a number to itself (provided there are enough bits to represent the result) is equivalent to shifting the repri'scnUiiun one bit position to the left.

2.5.2 Sign-Extension

It is o f t e n u s e f u l t o r e p r e s e n t a s m a l l n u m b e r w i t h f e w e r b i t s . F o r e x a m p l e , r a t h e r t h a n r e p r e s e n t t h e v a l u e 5 a s 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 , t h e r e a r e t i m e s w h e n it i s u s e f u l

2.5 Operations on Bits—Part I : Arithmetic

to allocate only six bits to represent the value 5:000101. There is little confusion, since we are all used to adding leading zeros without affecting the value of a number. A check for $456.78 and a check for $0000456.78 are checks having the same value.

What about negative representations ? We obtained the negative representation from its positive counterpart by complementing the positive representation and adding 1. Thus, the representation for —5, given that 5 is represented as 000101, is 111011. If 5 is represented as 0000000000000101, then the representation for

—5 is 1111111111111011. In the same way that leading 0s do not affect the value of a positive number, leading Is do not affect the value of a negative number.

In order to add representations of different lengths, it is first necessary to represent them with the same number of bits. For example, suppose we wish to add the number 13 to - 5 , where 13 is represented as 0000000000001101 and

—5 is represented as 111011. If we do not represent the two values with the same number of bits, we have

0000000000001X01 + 111011

When we attempt to perform the addition, what shall we do with the missing bits in the representation for —5? If we take the absence of a bit to be a 0, then we are no longer adding —5 to 13. On the contrary, if we take the absence of bits to be 0s, we have changed the —5 to the number represented as 0000000000111011, that is +59. Not surprisingly, then, our result turns out to be the representation for 72.

However, if we understand that a six-bit —5 and a 16-bit —5 differ only in the number of meaningless leading Is, then we first extend the value of —5 to 16 bits before we perform the addition. Thus, we have

0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 + 1111111111111011

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

and the result is +8, as we should expect.

The value of a positive number does not change if we extend the sign bit 0 as many bit positions to the left as desired. Similarly, the value of a negative number does not change by extending the sign bit 1 as many bit positions to the left as desired. Since in both cases, it is the sign bit that is extended, we refer to the operation as Sign-EXTension, often abbreviated SEXT. Sign-extension is performed in order to be able to operate on bit patterns of different lengths. It does not affect the values of the numbers being represented.

2.5.3 Overflow

Up to now, we have always insisted that the sum of two integers be small enough to be represented by the available bits. What happens if such is not the case?

You are undoubtedly familiar with the odometer on the front dashboard of your automobile. It keeps track of how many miles your car has been driven—but only up to a point. In the old days, when the odometer registered 99992 and you

drove it 100 miles, its new reading became 00092. A brand new car! The problem, as you know, is that the largest value the odometer could store was 99999, so the value 100092 showed up as 00092. The canyout of the ten-thousands digit was lost. (Of course, if you grew up in Boston, the carryout was not lost at all—it was in full display in the rusted chrome all over the car.)

We say the odometer overflowed. Representing 100092 as 00092 is unac-ceptable. As more and more cars lasted more than 100,000 miles, car makers felt the pressure to add a digit to the odometer. Today, practically all cars overflow at

1,000,000 miles, rather than 100,000 miles.

The odometer provides an example of unsigned arithmetic. The miles you add are always positive miles. The odometer reads 000129 and you drive 50 miles.

The odometer now reads 000179. Overflow is a carry out of the leading digit.

In the case of signed arithmetic, or more particularly, 2's complement arithmetic, overflow is a little more subtle.

Let's return to our five-bit 2's complement data type, which allowed us to represent integers from —16 to +15. Suppose we wish to add + 9 and +11. Our arithmetic takes the following form:

01001 01011 10100

Note that the sum is larger than +15, and therefore too large to represent with our 2's complement scheme. The fact that the number is too large means that the number is larger than 01111, the largest positive number we can represent with a five-bit 2's complement data type. Note that because our positive result was larger than +15, it generated a carry into the leading bit position. But this bit position is used to indicate the sign of a value. Thus detecting that the result is too large is an easy matter. Since we are adding two positive numbers, the result must be positive. Since the ALU has produced a negative result, something must be wrong. The thing that is wrong is that the sum of the two positive numbers is too large to be represented with the available bits. We say that the result has overflowed the capacity of the representation.

Suppose instead, we had started with negative numbers, for example, —12 and —6. In this case our arithmetic takes the following form:

10100 11010 O H I O

Here, too, the result has overflowed the capacity of the machine, since - 1 2 - 1 — 6 equals —18, which is "more negative" than —16, the negative number with the largest allowable magnitude. The ALU obliges by producing a positive result.

Again, this is easy to detect since the sum of two negative numbers cannot be positive.

2.6 Operations on Bits—Part I I : Logical Operations 56

Note that the sum of a negative number and a positive number never presents a problem. Why is that? See Exercise 2.25.

在文檔中 introduction to computing systems (頁 52-56)