Sie sind auf Seite 1von 5

Computer Organization I CMSC 2833 Purpose: simplifies binary subtraction

Complements Lecture 5

Binary subtraction requires: 1. complement 2. fixed field widths for binary numbers Ones complement: invert all bits Example: consider 10011110, the ones complement is 01100001 Think of the ones complement as the difference between the initial operand and a number of equal length having a one in every position. Example: 11111111 -10011110 01100001 Twos complement is one more than the ones complement Example: find the twos complement of 10011110 1. Find the ones complement 2. Add one to find the twos complement

01100001 01100001 + 1 01100010

Computer Organization I CMSC 2833 Twos Complement Representation 1. 2.

Complements Lecture 5

Choose a field width. Common field widths are 4, 8, 16, 32, and 64 bits. A binary number is positive if the most significant digit is zero (0), otherwise it is negative. Example: Find the decimal equivalent of the following 16-bit twos complement number 1001 1100 0000 0101

1.

Make the twos complement number positive. Find the magnitude of the twos complement. 1.1. First, find the ones complement by inverting all the bits. 1001 0110 1100 0011 0000 1111 0101 1010

1.2. Next, add one (1) to find the twos complement. 0110 0110 2. 0011 0011 1111 1111 1010 +1 1011

Convert to decimal. 2.1. First convert to hexadecimal. 0110 6 0011 3 1111 F 1011 B

2.2. Next, convert to decimal. Hex 6 3 F B Dec. 6 3 15 11

16 2 16 1 16 0 16

24576 768 240 11 25595

Computer Organization I CMSC 2833 Complements of Numbers rs complement

Complements Lecture 5

( N ) r ,c r n N if N 0,0 if N = 0 where n = number of digits in the integer portion of N r = radix


Example: Find the tens complement of 147

(147)10,c n = 3, r = 10, (147)10,c = 10 3 147 = 853


Example: Find the tens complement of 0.53

(0.53)10,c n = 0, r = 10, (0.53)10,c = 10 0 0.53 = 0.47


Example: Find the twos complement of 1010

(1010) 2,c n = 4, r = 2, (1010) 2,c = 2 4 1010 2 = 0110


24 =
10000 1010 0110 -

Example: Find the twos complement of 1010.101

(1010.101) 2,c , n = 4, r = 2

24 =

10000.000 1010.101 0101.011

Computer Organization I CMSC 2833 Subtraction with rs complement

Complements Lecture 5

M and S are two positive base r numbers were r must be evenly divisible by 2. Find the difference D=M S
1. Define a field width and add M to the r ' s complement of S . Discard any digits that carry into positions more significant than those defined by the field width. Discard any carry out digits. Example Find the difference (1010 0111) Find the 2s complement of

S = 0111 S
0110 1000 + 1 1001

1s complement of S Add 1 to find 2s complement of 2s complement of S Add M to the

r ' s complement of S . 1010 M +1001 2s complement of S 10011 D=M S


Final result 0011

10 -7 3 3

2.

Determining the sign. 2.1. If the most significant digit, d complement the number. Example Find the difference (0111 1010) Find the 2s complement of

r , then the number is negative, otherwise it is positive. 2 2.2. To find the magnitude of a negative number represented in r ' s complement form,

S = 0111

1s complement of S Add 1 to find 2s complement of 2s complement of S Add M to the

1010 0101 + 1 0110

r ' s complement of S . M 2s complement of S D=M S


1s complement 2s complement

0111 +0110 1101 0010 0011

7 -10 -3 -3

3.

Determining if overflow occurred. 3.1. Overflow occurs when adding two positive numbers or when adding two negative numbers. When the magnitude of the sum exceeds the range of values that can be represented in the field an overflow has occurred.

Computer Organization I CMSC 2833

Complements Lecture 5

Example: Consider 10s complement representation and a 2-digit field. A number, n, ranges over the interval, 50 an overflow has occurred.

n 49. When the sum is greater than 49 or less than -50

3.2. Special case: Overflow in 2s complement numbers. Overflow occurs when the carry into the sign bit is unequal to the carry out.

Example: consider a 4-bit 2s complement number. A number, n, ranges over the interval,

8 n 7. We consider two possibilities.


Decimal Binary 2s Complement 10 1011 1100 10111 2s Complement 01 0101 0100 01001

-5 -4 -9 Decimal

0101 0100

Binary

5 +4 9

0101 0100

Practice Problems. 1. Convert the following decimal numbers to binary and find the 1s and 2s complements. a. 15 b. 123 c. 846 d. 115.74 e. 349.786 2. Perform the subtraction of the following decimal numbers by using 9s and 10s complements. a. 6251 433 b. 834 562 c. 3674 598 d. 347 491 3. Using 1s and 2s complements perform the following subtractions a. 100110 11011 b. 1101010 110100 c. 10011.1101 101.11 d. 1010 - 11011 4. Find the following differences using 2s complement arithmetic. First convert decimal values to corresponding binary values. Next, find the twos complement representation of the subtrahend. Add the minuend and 2s complemented subtrahend. Check your answers. a. 12 6 b. 4 6 c. 3.125 6.5 d. 67.25 83.125

Das könnte Ihnen auch gefallen