Chapter 3 Complements Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
Subtraction using addition Conventional addition (using carry) is easily implemented in digital computers. However; subtraction by borrowing is difficult and inefficient for digital computers. Much more efficient to implement subtraction using ADDITION OF the COMPLEMENTS of numbers.
Complements of numbers (r-1 )’s Complement Given a number N in base r having n digits, the (r- 1)’s complement of N is defined as (r n - 1) - N For decimal numbers the base or r = 10 and r- 1= 9, so the 9’s complement of N is (10 n -1)-N 99999……. - N Digit n Digit n-1 Next digit First digit
2- Find the 9’s complement of and The 9’s complement of is = and the 9’s complement of is = ’s complement Examples
l’s complement For binary numbers, r = 2 and r — 1 = 1, r-1’s complement is the l’s complement. The l’s complement of N is (2n - 1) - N. Digit n Digit n-1 Next digit First digit Bit n-1Bit n-2…….Bit 1Bit 0 -
l’s complement Find r-1 complement for binary number N with four binary digits. r-1 complement for binary means 2-1 complement or 1’s complement. n = 4, we have 2 4 = (10000) 2 and = (1111) 2. The l’s complement of N is ( ) - N. = (1111) - N
The complement 1’s of is The 1’s complement of is l’s complement
r’s Complement Given a number N in base r having n digits, the r’s complement of N is defined as r n - N. For decimal numbers the base or r = 10, so the 10’s complement of N is 10 n -N ……. - N Digit n Digit n-1 Next digit First digit
10’s complement Examples Find the 10’s complement of and The 10’s complement of is = and the 10’s complement of is = Notice that it is the same as 9’s complement
For binary numbers, r = 2, r’s complement is the 2’s complement. The 2’s complement of N is 2 n - N. 2’s complement Digit n Digit n-1 Next digit First digit
2’s complement Example The 2’s complement of is The 2’s complement of is
Fast Methods for 2’s Complement Method 1: The 2’s complement of binary number is obtained by adding 1 to the l’s complement value. Example: 1’s complement of is (invert the 0’s and 1’s) 2’s complement of is =
Fast Methods for 2’s Complement Method 2: The 2’s complement can be formed by leaving all least significant 0’s and the first 1 unchanged, and then replacing l’s by 0’s and 0’s by l’s in all other higher significant bits. Example: The 2’s complement of is Leave the two low-order 0’s and the first 1 unchanged, and then replacing 1’s by 0’s and 0’s by 1’s in the four most significant bits.
Examples –Finding the 2’s complement of ( ) 2 Method 1 – Simply complement each bit and then add 1 to the result. ( ) 2 [N] = 2’s complement = 1’s complement ( ) 2 +1 =( ) 2 Method 2 – Starting with the least significant bit, copy all the bits up to and including the first 1 bit and then complement the remaining bits. N = [N] =
Subtraction of Unsigned Numbers using r’s complement Subtract N from M : M – N r’s complement N (rn – N ) add M to ( rn – N ) : Sum = M + ( r n – N) take r’s complement (If M N, the negative sign will produce an end carry rn we need to take the r’s complement again.)
Subtraction of Unsigned Numbers using r’s complement (1) if M N, ignore the carry without taking complement of sum. (2) if M < N, take the r’s complement of sum and place negative sign in front of sum. The answer is negative.
Example 1 (Decimal unsigned numbers), perform the subtraction = M > N : “Case 1” “Do not take complement of sum and discard carry” The 10’s complement of is Therefore: M = ’s complement of N = Sum= Discard end carry 10 5 = Answer = 59282no complement
Example 2; Now consider an example with M <N. The subtraction produces negative Using the procedure with complements, we have M = ’s complement of N = Sum = Take 10’s complement of Sum = The number is : Place negative sign in front of the number:-59282
Gray code هذه الشفرة تسمى " منعكسة " ( كما فى المرآة ) البدء بالرقمين صفر وواحد ثم عكسهما واحد وصفر وهكذا كما فى الجدول المرفق، وللإيضاح وضعت ألوان توضح صفر - واحد بالأصفر والعكس بلون آخر هذا فى خانة الآحاد.