Uploaded by Umang Gupta

2s sub add

advertisement
To obtain a number in it’s opposite sign, the 2’s complement form is used :
For example, a positive number is converted to it’s negative and a negative turns to it’s positive.
So, for taking 2’s complement of a number use the following steps :
a) Take 1s complement of the number. For this convert all 0s to 1 and all 1s to 0.
as, 1s complement of 1100 will be : 0011
b) Now, add 1 to the 1s complement obtained above. Rules for adding 0 and 1 are :
1+0=0+1=1
0+0=0
1 + 1 = 0 (with a carry 1)
so, add 1 to 0011 =
0011
+1
0100
Now, for subtracting 2 binary numbers using 2s complement the idea is that the subtrahend is
subtracted from the minuend. During the subtraction of 2 from 5 :
5–2=3
2 is the subtrahend and 5 is the minuend, subtraction gives a result as 3.
 For converting decimal to binary the procedure is to divide the number by 2 and keep the
remainders. The final result would be inverse of all remainders.
 To convert binary to decimal simply start from rightmost bit to leftmost bit and whichever is 1
take that position as power of 2 and add all such powers of 2 for result. Example:
Digit position:
:3210
Number_to_ be_converted : 1 1 0 0
Only 2nd and 3rd positions are 1, so 2^2 + 2^3 = 4 + 8 = 12
 To perform operations on 8 digit numbers add 0s to the front of the number. So, if number is
001 in 8 digit write remaining 0s to the front. Since, there are 3 digits in 001 so to make it 8 digit
5 more digits are required, so add 5 zeroes to 001, it becomes : 00000001.
ADDITION:
So, to do this using 2s complement first convert decimal to binary and simply add both numbers.
SUBTRACTION:
So, to do this using 2s complement first convert decimal to binary and simply take 2s complement of the
subtrahend and add it to the minuend. Which will be similar to :
Minuend + (-Subtrahend) = Minuend + 2s_complement(Subtrahend) = result
The above result can be either positive or negative based on whether subtrahend was more than
Minuend. So, to detect this in 2s complement the method is :
-> Check whether a carry was generated. If a carry was generated then the result is positive and
final answer was obtained while the carry is discarded.
-> Else if no carry was generated then the result is negative and take 2s complement of result
again to obtain the final answer.
a) 17 + 22 =
convert 17 to binary :
Divisor
2
2
2
2
2
2
Dividend
17
8
4
2
1
0
Remainder
1
0
0
0
1
So, 17 in binary becomes reverse of remainder column : 10001
convert 22 to binary :
Divisor
2
2
2
2
2
2
Dividend
22
11
5
2
1
0
So, 22 in binary becomes reverse of remainder column : 10110
So, 17 + 22 = 10001 + 10110 =
Remainder
0
1
1
0
1
00010001
+ 00010110
00100111
A carry was not generated so the result is obtained.
00100111 in decimal:
Digit position: 76543210
Number: 00100111
Positions with 0, 1, 2 and 5 are ones. So,
= 2^5 + 2^2 + 2^1 + 2^0
= 32 + 4 + 2 + 1
= 39
b) 17 + (-22) = 17 – 22
So, this is subtraction, take 2s complement of subtrahend (22) to convert it to -22, then add the 2s
complement to minuend (17):
22 in binary : 00010110
1s complement of 22 = 11101001
2s complement of 22 = 11101001
+1
11101010
00010001
+ 11101010
11111011
result is negative as no carry is generated. This is because subtrahend was greater than minuend.
So, take 2s complement of 11111011 as:
1s complement of 11111011 = 00000100
2s complement of 11111011 = 00000100 + 1 = 00000101
So, result is -00000101.
Download