I'm reading through a computer architecture book and came across the following:
Assume you have a 8-bit cell. So there are 256 possible integer values. Non-negatives run from 0 to 127. Assuming two's complement binary representation, what is the sum of 97 + 45?
Unsigned is clearly 142, you can do it as 97 + 45 in decimal, or:
0110 0001
0010 1101 ADD
--------------
1000 1110
But when you perform two's complement, you take that result (1000 1110
) and determine that it is negative since the sign bit is 1. Then take the one's complement of it:
NOT 1000 1110 = 0111 0001
Then determine its two's complement:
0111 0001
0000 0001 ADD
--------------
0111 0010
This number is 114
but because our original number had a 1
in the sign bit, it's -114
.
Question(s):
Why go through all the trouble of having the two's complement to find -114
? Since 97 and 45, why not just find the sum of the two positive integers as an unsigned value which fits within the range of an 8-bit cell (1111 1111
being 255
). Is it just because the question asks for the two's complement?
Is -114
equivalent to 142
? I believe so if you take the two's complement number line, yo get 142-256
which is -114
. From this, I dont understand why you would want to even use two's complement if you are summing two positive values!
A 1's complement just means flip all the bits, a 2's complement means negate the value. So 1's complement for 8-bit results effectively in 255 - x
and 2's in 256 - x
. You achieve the result of a 2's complement by doing a 1's complement and adding 1
.
The 142
in 8-bit is equal to -114
. Don't get too confused about it.