CPE 201 Digital Design Lecture 3: Digital Systems & Binary Numbers (3)
2 Lecture Outline Complement systems Arithmetic operations with binary numbers
3 Complements Goal: logicalmanipulation –simplify subtraction operation and logical manipulation Complement systems negate a number by taking its complement Two types of complements for each base-r system –Radix complement: r’s complement –Diminished radix complement: (r-1)’s complement Big advantage: allows us to perform subtraction using addition –Thus, only need adder component, no need for separate subtractor component!
4 Diminished Radix Complement The (r-1)’s complement of an n-digit number N = r n – 1 – N Decimal –r = 10 9’s complement of N is 10 n – 1 – N –10 n : a “1” followed by n “0”s –10 n – 1: a number given by n “9”s The 9’s complement of a digital number is obtained by subtracting each digit from 9
5 Examples: 9’s Complement E.g.: 9’s complement of : – = : – = : =
6 Diminished Radix Complement – Radix 2 Binary –r = 2 1’s complement of N is 2 n – 1 – N –2 n : a “1” followed by n “0”s –2 n – 1: a number given by n “1”s The 1’s complement of a binary number is obtained by subtracting each digit from 1 Observation: 1-0 = 11-1 = 0 –Bit changes! The 1’s complement of a binary number is obtained by flipping the bits of the number
7 Examples: 1’s complement E.g.: 1’s complement of : : : : : :
8 Radix Complement The r’s complement of an n-digit number N = r n – N This means just adding 1 to the (r-1)’s complement! Decimal –Leave all LSB zeros unchanged –Subtract the first nonzero LSB from 10, subtract the rest from 9 Binary –Leave all LSB zeros and the first “1” unchanged –Flip the rest of the bits
9 Examples: 10’s Complement With addition: (1289, 2389, 5840, 3048) 1289: ( ) + 1 = = : ( ) + 1 = = : ( ) + 1 = = :( ) + 1 = = 6952 With alternative rule 1289: : : :6952
10 Examples: 2’s Complement With addition: ( , , , ) : = : = : = : = With alternative rule: ( , ) : : Carry ignored
11 Subtraction with r’s Complements Subtracting a number-done by adding its complement –Adding its complement results in answer exactly 10 more –Drop the 1 results in subtracting using addition only - - Exemplified with base 10 and 1-digit numbers
12 Subtraction with r’s Complements Compute M – N M – N = M + r’s complement (N) = M + r n – N = (M – N) + r n M>N: = (M – N) + r n M<N: = r n – (N – M) Carry to next digit: IGNORED! The r’s complement of the desired result with negative sign! Take the complement and add a negative sign.
13 Subtraction with r’s Complements To compute M – N: –Take the r’s complement of N –Add that value to M –Two cases can occur: A carry to next digit is obtained (M was greater than N) ignore the carry to get the result No carry is obtained (M was smaller than N) take the r’s complement of the resulted value and add a negative sign
14 Subtraction Using 10’s Complement – 3250 = Result = – = Res. = -10’s complement(30718) = Discard the carry No carry Make sure you fill out to the maximum available number of digits! 0
15 Subtraction Using 2’s Complement X = Y = X – Y = ?Y – X = ? X – Y:Y – X: Res. = -2’s comp. ( ) = Discard the carry No carry
16 Subtraction with (r-1)’s Complements Compute M – N M – N = M + (r-1)’s complement (N) = M + r n – N – 1 = (M – N – 1) + r n M>N: = (M – N – 1) + r n M<N: = r n – 1 – (N – M) Carry to next digit: IGNORED! Got 1 less than result add 1 The (r-1)’s complement of the desired result with negative sign! Take the complement and add a negative sign.
17 Subtraction with (r-1)’s Complements To compute M – N: –Take the (r-1)’s complement of N –Add that value to M –Two cases can occur: A carry to next digit is obtained (M was greater than N) ignore the carry and add 1 to get the result No carry is obtained (M was smaller than N) take the (r-1)’s complement of the resulted value and add a negative sign
18 Subtraction Using 1’s Complement X = Y = X – Y = ?Y – X = ? X – Y:Y – X: ’s complement of = Discard the carry No carry
19 Readings Chapter 1: –Sections 1.6, 1.7