二进制加法。是不是溢出了?

发布于 2024-08-30 16:51:59 字数 94 浏览 6 评论 0原文

二进制值采用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以笙箫默 2024-09-06 16:51:59

这是溢出,你的教授是正确的。您正在存储更多可以在分配的空间中保存的位(即使这些位代表的数字是负数)。

下溢是指通过大数学移位而将位归零。在定点数学中很常见。用一个非常小的数除以一个非常大的数,通常会得到 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.

何处潇湘 2024-09-06 16:51:59

有一个很好的例子讨论了添加两个寄存器的内容 -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.

酒绊 2024-09-06 16:51:59

编辑:我刚刚意识到-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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文