二进制加法。是不是溢出了?
二进制值采用 2 补码形式。
如果我要将 110001 (-15) 和 101110 (-18) 相加,并且答案必须存储在 6 位整数中,这是否是下溢/上溢。
Binary values are in 2s Complement form.
If I am to add 110001 (-15) and 101110 (-18), and the answer has to be stored in a 6-bit integer, is this an underflow/overflow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是溢出,你的教授是正确的。您正在存储更多可以在分配的空间中保存的位(即使这些位代表的数字是负数)。
下溢是指通过大数学移位而将位归零。在定点数学中很常见。用一个非常小的数除以一个非常大的数,通常会得到 0。这就是下溢。
This is overflow, your professor is correct. You are storing more bits that can be held in the alloted space (even though the number that the bits represent is negative.)
Underflow is when bits get zero'd out through shifting on big math. Very common in fixed point math. Divide a very small number by a very big number and you will quite often get a 0. That is underflow.
有一个很好的例子讨论了添加两个寄存器的内容 -70 和 -90 并将其存储在第三个寄存器中。
我们假设
R1[8 位] = -70
R2[8 位] = -90
结果[9位] = -160,溢出的额外位。
此示例在参考文献1 下面。
根据本页讨论的规则,该示例可以缩放为任意两个带符号的十进制数。
There is a a good example discussed to add two registers with contents -70 and -90 and store it in third register.
Let's assume
R1[8 bits] = -70
R2[8 bits] = -90
Result[9 bits] = -160, an extra bit for overflow.
This example is discussed on overflow_signed_detection page on ref 1 below.
With the rules discussed on this page, the example can be scaled to any two signed decimal numbers.
编辑:我刚刚意识到-33对于6位来说太大了,所以结果不是-33而是+31,因此它绝对是溢出:)
添加两个数字并得到正确的结果如果绝对不是溢出。溢出的一个示例是将两个负数相加并得到一个正数作为结果(反之亦然)。
例如,如果将两个正数 0x7fffffff 和 0x00000001 相加,则会得到负数 0x80000000,这肯定是错误的,因此会溢出。
也许您将溢出与进位混淆了?
EDIT: I just realized that -33 is too large for 6 bits, so the result is NOT -33 but +31, and thus it is definitely an overflow :)
Adding two numbers and getting the correct result if definitely NOT an overflow. An example of an overflow is adding two negative numbers and getting a positive number as a result (or vice versa).
For example, if you add the two positive numbers 0x7fffffff and 0x00000001, you get the negative number 0x80000000, which is definitely wrong and thus an overflow.
Maybe you are confusing overflow with carry?