In: Computer Science
using 8 bits and 2s complement integer arithmetic, show how a processor would calculate 63 - 17
Number: 63
Let's convert this to two's complement binary
63
Since this is a positive number. we can directly convert this into
binary
Divide 63 successively by 2 until the quotient is 0
> 63/2 = 31, remainder is 1
> 31/2 = 15, remainder is 1
> 15/2 = 7, remainder is 1
> 7/2 = 3, remainder is 1
> 3/2 = 1, remainder is 1
> 1/2 = 0, remainder is 1
Read remainders from the bottom to top as 111111
So, 63 of decimal is 111111 in binary
so, 63 in 2's complement binary is 00111111
Number: -17
Let's convert this to two's complement binary
-17
This is negative. so, follow these steps to convert this into a 2's
complement binary
Step 1:
Divide 17 successively by 2 until the quotient is 0
> 17/2 = 8, remainder is 1
> 8/2 = 4, remainder is 0
> 4/2 = 2, remainder is 0
> 2/2 = 1, remainder is 0
> 1/2 = 0, remainder is 1
Read remainders from the bottom to top as 10001
So, 17 of decimal is 10001 in binary
So, 17 in normal binary is 00010001
Step 2: flip all the bits. Flip all 0's to 1 and all 1's to
0.
00010001 is flipped to 11101110
Step 3:. Add 1 to above result
11101110 + 1 = 11101111
so, -17 in 2's complement binary is 11101111
Adding 00111111 and 11101111 in binary
00111111
11101111
-------------
(1)00101110
-------------
Sum produces a carry of 1. We can ignore that carry.
So, sum of these numbers in binary is 00101110
Verification:
---------------
sum = 00101110
since left most bit is 0, this number is positive
so, we can directly convert this into a decimal value
Converting 101110 to decimal
101110
=> 1x2^5+0x2^4+1x2^3+1x2^2+1x2^1+0x2^0
=> 1x32+0x16+1x8+1x4+1x2+0x1
=> 32+0+8+4+2+0
=> 46
Answer: 46
This is correct since we can verify that 63+-17 = 46
So, there was no overflow.