Digital Logic and Design
Dr. M. edit Master subtitle style Click toNajam ul Islam PhD Electrical Engineering
Lecture 3: Ns Complement
22
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
33
100011002 = -1210 Sign bit Magnitude
Magnitude
Formal Definition of Complement
Complements are used in digital computers for simplifying
subtraction operation.
There are two types of Complements
ns Complement (n-1)s Complement
where n is the base
44
Complements
In base-10:
10s Complement 9s Complement 2s Complement 1s Complement
Remember the rule: Ns complement is +1 (add 1) to the (N-1)s complement.
In base-2
55
10s Complement
Given a positive number N in base n with an integer
part of r digits, then ns complement is defined as n - N 10s complement of (52520) is 10 - 52520 = 100000 52520 = 47480 Similarly, 10s complement of 134795 is 865205
66
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
77
2s Complement
2s Complement of (101100) is
(2) (101100) = (1000000 101100) = 010100 2s Complement of (0.0110)2 is (20) (0.0110) = (1.0000 0.0110) = 0.1010 Rules for binary Subtraction 0-0=0 0 - 1 = 1, and borrow 1 from the next more significant bit 1-0=1 1-1=0 (26)10 = (64)10 = (1000000)2 (20)10 = (1)10 = (1)2
88
Twos Complement Representation
The twos complement of a binary number involves inverting
all bits and adding 1.
For an n bit number N the 2s complement is (2n 1) N + 1. Called radix complement by Mano since 2s complement for
base (radix 2). To find negative of number (represented in 2s complement), take the 2s complement.
2s comp of 00110011 is 11001101 2s comp of 10101010 is 01010110
Sign bit 99
000011002 = 1210 Magnitu de
Sign bit
111101002 = -1210 Magnitu de
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 1 --------------01100101 01100100
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
1010
(n-1)s Complements
Given a positive number N in base n with an integer
part of r digits and a fraction part of m digits then (n1)s complement is defined as n- n-m - N
Need to accommodate the fraction part through n-m 9s complement of (52520) is (10 10 52520) = 100000 1 52520 = 47479
Remember the rule: Ns complement is +1 unit (add 1) to the (N1)s complement.
1111
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 10s complement = 685300 zeros unchanged. Then subtract the first non-zero digit from 10 and all the remaining digits from 9s.
A version of Algorithm 2 on Slide 6, for binary numbers
To find the 10s complement of a decimal number leave all leading
1212
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 = nr n-m
1313
20 24 = (1-0.0625) = (0.9375)10 = (0.1111)2 Use method of Example 1.3
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 a number (represented in 1s complement), take the 1s complement.
000011002 = 1210 Magnitu de 111100112 = -1210 Magnitu de
Sign bit 1414
Sign bit
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)
1515
Signed Binary Numbers
1616
1s Complement Addition
Using 1s complement numbers, adding numbers is easy. For example, suppose we wish to add +(1100)2 and +(0001)2. Lets compute (12)10 + (1)10. (12)10 = +(1100)2 = 011002 in 1s comp. (1)10 = +(0001)2 = 000012 in 1s comp.
0 1 1 0 0 + 0 0 0 0 1 Ad -------------d 0 0 1 1 0 1 0 Add carry -------------0 1 1 0 1 Final
Resu lt
1717
Step 1: Add binary numbers Step 2: Add carry to loworder bit
2s Complement Addition
Using 2s complement numbers, adding numbers is easy. For example, suppose we wish to add +(1100)2 and
+(0001)2. Lets compute (12)10 + (1)10. (12)10 = +(1100)2 = 011002 in 2s comp. (1)10 = +(0001)2 = 000012 in 2s comp.
Ad d Final Res ult
Step 1: Add binary numbers Step 2: Ignore carry bit
0 1 1 0 0 + 0 0 0 0 1 -------------0 0 1 1 0 1
1818
Igno re
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
1919
Subtraction with ns Complements
The subtraction of two positive number (M-N), both of
base-n may be done as follows. subtrahend N.
1. Add the minuend M to the ns complement of the 1. 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 ns complement of the
number obtained in step 1, and place a negative sign in front.
2020
2s Complement Subtraction
Using 2s complement numbers, follow steps for subtraction For example, suppose we wish to subtract +(0001)2 from +(1100)2. Lets compute (12)10 - (1)10.
(12)10 = + (1100)2 = 011002 in 2s comp. (-1)10 = - (0001)2 = 111112 in 2s comp.
0 1 1 0 0 0 0 0 0 1 --------------
Step 1: Take 2s complement of 2nd operand Step 2: Add binary numbers Step 3: Ignore carry bit
2s 0 1 1 0 0 comp + 1 1 1 1 1 Ad -------------d 1 0 1 0 1 1 Final Res ult Igno re Carr
2121
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
0 1 1 0 1 carry + 1 1 0 1 1 -------------Discarding the carry bit, the sign bit is seen to0be zero, 1 0 1 0 0
indicating a correct result. Indeed, (01000)2 = +(1000)2 = +(8)10.
2222
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.
2323
Exercise
Using 10s complement subtract 3250 from 72532
M = 72532 N = 03250
10s complement of N = 96750
72532 + 96750 = 169282 Answer = 69282
2424
Exercise
Subtract (3250 72532) using 10s Complement.
M = 03250 N = 72532
10s complement of N = 27468
03250 + 27468 = 030718
So again take 10s complement of 30718 and place - sign
Answer = - 69282
2525
Exercise
Using 2s complement subtract 1000100 from 1010100
M = 1010100 N = 1000100
2s complement of N = 0111100
1010100 + 0111100 = 10010000 Answer = 10000
2626
Exercise
Subtract (1000100 1010100) using 2s Complement
M = 1000100 N = 1010100
2s complement of N = 0101100
1000100 + 0101100 = 01110000
So again take 2s complement of 1110000 and place a sign Answer = - 10000
2727
Subtraction with (n-1)s Complements
The subtraction of two positive number (M-N), both of
base-n may be done as follows. subtrahend N.
1. Add the minuend M to the (n-1)s complement of the 1. 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 (n-1)s complement of
the number obtained in step 1, and place a negative sign in front.
2828
1s Complement Subtraction
Using 1s complement numbers, subtracting numbers is also easy. For example, suppose we wish to subtract +(0001)2 from +(1100)2. Lets compute (12)10 - (1)10. 0 1 1 0 0 (12)10 = +(1100)2 = 011002 in 1s comp. - 0 0 0 0 1 (-1)10 = -(0001)2 = 111102 in 1s comp. -------------1s comp Ad d
Step 1: Take 1s complement of 2nd operand Step 2: Add binary numbers Step 3: Add carry to low order bit Add carry Final Res ult
2929
0 1 1 0 0 + 1 1 1 1 0 -------------1 0 1 0 1 0 1 -------------0 1 0 1 1
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
3030
Exercise
Subtract (3250 72532) using 9s Complement.
M = 03250 N = 72532
10s complement of N = 27467
03250 + 27467 = 030717
So again take 9s complement of 30717 and place - sign
Answer = -69282
3131
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
3232
Exercise
Subtract (1000100 1010100) using 1s Complement
M = 1000100 N = 1010100
1s complement of N = 0101011
1000100 + 0101011 = 01101111
So again take 1s complement of 1101111 and place - sign
Answer = -10000
3333
Sample Quiz
Convert the following into Binary, Hexadecimal & Octal.
53.0286 106.274
Write the 10s Complement of 786.543 &
9s Complement of 12345
3434