问题
I am trying to add 24 to 10 in 2's complement. I found 24 in 2's complement to be: 011000 and 10 in 2's complement to be 001010. When I add the two together I get: 100010. The result is a negative number. Is this an example of overflow? Is it not possible to add 24 to 10 in 2's complement?
回答1:
If you only have 6 bits, then yes, it is an overflow. The reason is that 6-bit 2's complement can only store numbers -32..31
and your desired result, 34
, is outside that range.
If you had, say, 8 bits instead, your result would be 00100010
, which would not be a negative number, or an overflow.
Adding two positive numbers and coming up with a negative result, or two negative numbers and coming up with a non-negative result, is a definitive indicator of signed 2's complement overflow.
来源:https://stackoverflow.com/questions/35018541/twos-complement-addition-overflow