算术溢出
为什么用补码将正数和负数相加时不会发生算术溢出?如果可以的话,请提供一个 8 位有符号整数(字节)的示例。
Why is it that an arithmetic overflow cannot occur when adding an positive and a negative number using two's complement. If you could please provide an example with 8-bit signed integers (bytes).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有一个正数
A
和一个负数B
。它们的总和是S
。那么:他们的总和将在中间的某个地方。请注意,会有进位,但这不是溢出(不正确的和)。
Assume that you have a positive number
A
, and a negative numberB
. Their sum isS
. Then:Their sum would be somewhere in the middle. Note that there would be a carry, but that is not an overflow(incorrect sum).
这……听起来有点像家庭作业。您是想使用“作业”标签吗?
不能溢出的原因是,将正数
x
和负数y
相加将产生一个值z
,使得abs (z) <绝对值(x) 和 绝对值(z)
绝对值(y)。由于
x
和y
可以在不溢出的情况下表示,并且z
比任何一个都更接近零,z
也可以不溢出地表示。任何一对正数和负数都可以构成一个例子。
This ... kind of sounds like homework. Did you mean to use the 'homework' tag?
The reason you can't overflow is because adding a positive
x
and a negative numbery
will produce a valuez
such thatabs(z) < abs(x)
andabs(z) < abs(y)
. Sincex
andy
could be represented without overflow, andz
is closer to zero than either one,z
can also be represented without overflow.Any pair of positive and negative numbers form an example.