Вы находитесь на странице: 1из 34

Digital Logic and Design (aka DLD: EEE241)

Dr. Aamir M. Khan


PhD Embedded Systems

Lecture 3: Ns Complement

How To Represent Signed Numbers Plus and minus sign used for decimal numbers:

25 (or +25), -16, etc. For computers, desirable to represent everything as bits. Three types of signed binary number representations: signed magnitude, 1s complement, 2s complement. In each case: left-most bit indicates sign: positive (0) or negative (1).

Consider signed magnitude:


000011002 = 1210 Sign bit
3

100011002 = -1210 Sign bit Magnitude

Magnitude

Ones Complement Representation The ones complement of a binary number involves


inverting all bits. 1s complement of 00110011 is 11001100 1s complement of 10101010 is 01010101 For an n bit number N the 1s complement is (2n-1) N. Called diminished radix complement by Mano since 1s complement for base (radix 2). To find negative of 1s complement number, take the 1s complement.
000011002 = 1210
4

111100112 = -1210

Twos Complement Representation


The twos complement of a binary number involves inverting
all bits and adding 1.

2s comp of 00110011 is 11001101 2s comp of 10101010 is 01010110

For an n bit number N the 2s complement is (2n) N. Called radix complement by Mano since 2s complement for
base (radix 2). To find negative of 2s complement number, take the 2s complement.
000011002 = 1210
5

111101002 = -1210

Twos Complement Shortcuts


Algorithm 1 Simply complement each bit and then add 1 to the
result. Finding the 2s complement of (01100101)2 and of its 2s complement
N = 01100101 10011010 + 1 --------------10011011 [N] =
+

10011011 01100100 1 --------------01100101

Algorithm 2 Starting with the least significant bit, copy all of the
bits up to and including the first 1 bit and then complementing the remaining bits. N = 01100101 [N] = 10011011
6

Finite Number Representation Machines that use 2s complement arithmetic can


represent integers in the range -2n-1 N 2n-1 1 where n is the number of bits available for representing N. Note that 2n-1 1 = (01111)2 and -2n-1 = (10000)2 For 2s complement more negative numbers than positive. For 1s complement two representations for zero. For an n bit number in base (radix) r there are r n different unsigned values. (0, 1, r n-1)

Signed Binary Numbers

1s Complement Addition

Using 1s complement numbers, adding numbers is easy. For example, suppose we wish to add (12)10 + (1)10 Binary code for 12 is (1100)2 and for 1 is (0001)2. But in signed number form, we have 0 at the MSB for +ve numbers. (12)10 = +(1100)2 = 011002 in 1s comp. (1)10 = +(0001)2 = 000012 in 1s comp.

Step 1: Add binary numbers Step 2: Add carry to low-order bit

0 1 1 0 0 + 0 0 0 0 1 Add -------------0 0 1 1 0 1 0 Add carry -------------0 1 1 0 1 Final


Result

1s Complement Subtraction
Using 1s complement numbers, subtracting numbers is also easy. For example, suppose we wish to compute (12)10 - (1)10. Subtraction will be like: 12 + (-1) 0 1 1 0 0 (12)10 = +(01100)2 = 011002 in 1s comp. - 0 0 0 0 1 (-1)10 = - (00001)2 = 111102 in 1s comp. -------------1s comp Step 1: Take 1s complement of 2nd operand Step 2: Add binary numbers Step 3: Add carry to low order bit Add

Add carry Final Result


10

0 1 1 0 0 + 1 1 1 1 0 -------------1 0 1 0 1 0 1 -------------0 1 0 1 1

2s Complement Addition Using 2s complement numbers, adding numbers is easy. For example, suppose we wish to add (12)10 + (1)10. As both numbers are +ve, their binary codes will have 0 at
MSB. (12)10 = +(1100)2 = 011002 in 2s comp. (1)10 = +(0001)2 = 000012 in 2s comp.
Step 1: Add binary numbers Step 2: Ignore carry bit (if any)
Add Final Result

0 1 1 0 0 + 0 0 0 0 1 -------------0 1 1 0 1

11

2s Complement Subtraction

Using 2s complement numbers, follow steps for subtraction For example, suppose we wish to compute (12)10 - (1)10. Subtraction will be performed as: 12 + (-1) 0 1 1 0 0 Lets 0 0 0 0 1 (12)10 = + (01100)2 = 011002 in 2s comp. ------------- (-1)10 = - (00001)2 = 111112 in 2s comp.
2s comp Step 1: Take 2s complement of 2nd operand Step 2: Add binary numbers Step 3: Ignore carry bit Add Final Result

0 1 1 0 0 + 1 1 1 1 1 -------------1 0 1 0 1 1

Ignore Carry
12

2s Complement Subtraction: Example #2 Lets compute (13)10 - (5)10.


(13)10 = + (1101)2 = (01101)2 (-5)10 = - (0101)2 = (11011)2 Adding these two 5-bit codes
carry

0 1 1 0 1 + 1 1 0 1 1 -------------1 0 1 0 0 0

Discarding the carry bit, the sign bit is seen to be zero,


indicating a correct result. Indeed, (01000)2 = +(1000)2 = +(8)10.
13

2s Complement Subtraction: Example #3 Lets compute (5)10 - (12)10.


(-12)10 = -(1100)2 = (10100)2 (5)10 = +(0101)2 = (00101)2 Adding these two 5-bit codes

0 0 1 0 1 + 1 0 1 0 0 -------------1 1 0 0 1

Here, there is no carry bit and the sign bit is 1. This


indicates a negative result, which is what we expect. (11001)2 = -(7)10.
14

Formal Definition of Complement


Complements are used in digital computers for
simplifying subtraction operation.

There are two types of Complements


rs Complement (r-1)s Complement
where r is the base

15

Complements
In base-10:
10s Complement 9s Complement

In base-2
2s Complement 1s Complement
Remember the rule: rs complement is +1 (add 1) to the (r-1)s complement.

16

10s Complement
Given a positive number N in base r with an integer
part of n digits, then rs complement is defined as rn - N 10s complement of (52520) is 10 - 52520 = 100000 52520 = 47480 Similarly, 10s complement of 134795 is 865205

17

10s Complement
10s complement of (0.3267) = 10 0.3267
= 1 - 0.3267 = 0.6733

10s complement of (25.639) = 10 - 25.639


= 74.361
0.3267 has no integer part => 100

18

2s Complement
2s Complement of (101100) is
(2) (101100) = (1000000 101100) = 010100 (20) (0.0110) = (1.0000 0.0110) = 0.1010

2s Complement of (0.0110)2 is

Rules for binary Subtraction 0-0=0 0 - 1 = 1, and borrow 1 from the next more significant bit 1-0=1 (26)10 = (64)10 = (1000000)2 1-1=0 (20) = (1) = (1)
10 10 2
19

(r-1)s Complements
Given a positive number N in base r with an integer
part of n digits and a fraction part of m digits then (r1)s complement is defined as rn - r-m - N

Need to accommodate the fraction part through r-m 9s complement of (52520) is (10 10 52520) = 100000 1 52520 Remember the rule: rs complement is +1 = 47479 unit (add 1) to the (r-1)s
complement.
20

9s Complement
9s complement of (0.3267) is (10 10 0.3267)
= 0.9999 0.3267 = 0.6732 9s complement of (25.639) is (10 10 25.639) = 99.999 25.639 = 74.360 Find the 9s and 10s complements of 314700. 9s complement = 685299 A version of Algorithm 2 on 10s complement = 685300 Slide 6, for binary numbers

To find the 10s complement of a decimal number leave all leading


zeros unchanged. Then subtract the first non-zero digit from 10 and all the remaining digits from 9s.
21

1s Complement 1s complement of 110100101 is 001011010


which can be obtained by replacing each one by a zero and each zero by one.

1s Complement of (101100)2 is (26 - 1)10 (101100)2


= (1000000 1 101100)2 = 010011 1s Complement of (0.0110)2 is (20 2 4)10 (0.0110)2 = (0.1111 0.0110)2 = 0.1001
Remind it: 20 24 = rn n-m
22

20 24 = (1-0.0625) = (0.9375)10 = (0.1111)2 Use method of Example 1.3

Exercise Find the 1s and 2s complements of the binary number


1101001101

1s complement is 0010110010 2s complement is 0010110011

Find the 1s and 2s complements of 100010100


1s complement is 011101011 2s complement is 011101100
23

Subtraction with rs Complements The subtraction of two positive number (M-N), both of
base-r may be done as follows.

1. Add the minuend M to the rs complement of the


subtrahend N.

2. Inspect the result obtained above for an end carry:


a) If an end carry occurs, discard it and write the answer as it is b) If an end carry does not occur, then take rs complement of the
number obtained in step 1, and place a negative sign in front.

24

Exercise Using 10s complement subtract 3250 from 72532


M = 72532 N = 03250

10s complement of N = 96750


+ = Answer = 69282
25

72532 96750 169282

Exercise Subtract (3250 72532) using 10s Complement.


M = 03250 N = 72532

10s complement of N = 27468


+ =
03250 27468 030718

26

Exercise Using 2s complement subtract 1000100 from 1010100


M = 1010100 N = 1000100

2s complement of N = 0111100
1010100 0111100 10010000

+ = Answer = 10000
27

Exercise Subtract (1000100 1010100) using 2s Complement


M = 1000100 N = 1010100

2s complement of N = 0101100
+ =
1000100 0101100 01110000

28

Subtraction with (r-1)s Complements The subtraction of two positive number (M-N), both of
base-r may be done as follows.

1. Add the minuend M to the (r-1)s complement of the


subtrahend N.

2. Inspect the result obtained above for an end carry:


a) If an end carry occurs, add 1 to the number. b) If an end carry does not occur, then take (r-1)s complement of
the number obtained in step 1, and place a negative sign in front.
29

Exercise Using 9s complement subtract 3250 from 72532


M = 72532 N = 03250

9s complement of N = 96749
72532 + 96749 = 169281 Add this end carry to the above i.e. 69281 +1 Answer = 69282
30

Exercise Subtract (3250 72532) using 9s Complement.


M = 03250 N = 72532

10s complement of N = 27467


+ =
03250 27467 030717

31

Exercise Using 1s complement subtract 1000100 from 1010100


M = 1010100 N = 1000100

1s complement of N = 0111011
1010100 + 0111011 = 10001111 Add this end carry to the above i.e. 0001111 + 1 Answer = 10000
32

Exercise Subtract (1000100 1010100) using 1s Complement


M = 1000100 N = 1010100

1s complement of N = 0101011
+ =
1000100 0101011 01101111

33

Sample Quiz
Convert the following into Binary, Hexadecimal & Octal.
53.0286 106.274

Write the 10s Complement of 786.543 & 9s


Complement of 12345

34

Вам также может понравиться