这是溢出算术计算吗? [英] Is this an overflow arithmetic calculation?

查看:95
本文介绍了这是溢出算术计算吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以当我读这本书时,它说添加不同的符号并减去相同的符号就不会发生溢出. 但是我在执行此操作时有疑问:185-122 我将122的二进制转换为2s的补码,并进行了加法运算,这是不同的符号: 185 +(-122)并将它们加在一起时,符号位溢出到100111111.但是,如果我切断左侧的MSB,则是正确的答案.它溢出了吗?

So when I read the book and it says that overflow can't occur when add different signs and subtraction of the same sign. But I have question when I do this: 185 - 122 I converted binary of 122 to 2s complement and did the addition, which is different signs: 185+(-122) and when I add them together, I got the sign bit overflow to 100111111. But if I cut off the MSB on the left, it is the correct answer. Is it an overflow?

推荐答案

不,它不是溢出-必须仅丢弃MSB中添加2 1's所导致的溢出.来自维基百科

No, it isn't overflow - the overflow resulting from the addition of 2 1's in the MSB must just be discarded. From Wikipedia

要获得二进制数的二进制补码,请使用按位NOT运算将这些位取反或翻转".然后将1的值添加到结果值中,而忽略了取2的补数为0时发生的溢出.

To get the two's complement of a binary number, the bits are inverted, or "flipped", by using the bitwise NOT operation; the value of 1 is then added to the resulting value, ignoring the overflow which occurs when taking the two's complement of 0.

所以在您的示例中

185 10111001
122 01111010 -

取122的2的补码(1的补码+1)

Taking the 2's complement of 122 (One's complement +1)

01111010 => 10000110

添加:

10111001 185
10000110 +(-122)
--------
00111111 (63)

= 63

overflow被忽略.

但是,在执行2的补码后,有一些检测溢出的规则:

There are however rules for detecting overflow after doing the 2's complement :

  • 如果两个正数之和导致负数
  • 如果两个负数的总和为正数

这篇关于这是溢出算术计算吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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