二进制补码:加法溢出? [英] Two's Complement: Addition Overflow?

查看:56
本文介绍了二进制补码:加法溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在2的补码中加24到10.我发现2的补码中的24为:011000,2的补码中为10的为010010.将两者加在一起时,得到的是:100010.结果是负数.这是溢出的例子吗?不可能在2的补码中加24到10?

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?

推荐答案

如果只有6位,则是的,这是一个溢出.原因是6位2的补码只能存储数字 -32..31 ,而您想要的结果 34 不在该范围内.

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.

例如,如果您有8位,则结果将是 00100010 ,这不是负数,也不是溢出.

If you had, say, 8 bits instead, your result would be 00100010, which would not be a negative number, or an overflow.

加两个正数并得出负数,或两个负数并得出非负数是有符号2补码溢出的确定指标.

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.

这篇关于二进制补码:加法溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆