Two's Complement: Addition Overflow?

旧街凉风 提交于 2020-01-07 03:29:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!