如何在二进制补码中检测到溢出? [英] How is overflow detected in two's complement?

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

问题描述

我看到当我使用two's complement减去正数和负数时,会出现溢出.例如,如果我从2中减去1,我得到:

I see that when I subtract positive and negative number using two's complement I get overflows. For example, if I subtract 1 from 2 I get:

2 = 0010
1 = 0001 -> -1 = 1111
2 + (-1) -> 0010 + 1111 = 10001

所以这里的结果有左第五位10001-它溢出了吗? 我发现了用二进制补码检测到的溢出的这些规则:

So here the result has fifth left bit 10001 - is it overflow? I've found these rules for detected overflows with two's complement:

如果两个正数之和得出负数,则总和 已经溢出了.如果两个负数之和产生一个正数 结果,总和溢出了.否则,总和不会溢出.

If the sum of two positive numbers yields a negative result, the sum has overflowed. If the sum of two negative numbers yields a positive result, the sum has overflowed. Otherwise, the sum has not overflowed.

任何人都可以详细说明并举例吗?

Can anyone please elaborate on these and show example?

推荐答案

让我们从标题问题的答案开始.

Let's start with an answer to your title question.

如何在二进制补码中检测到溢出?

How is overflow detected in two's complement?

溢出规则:如果两个具有相同符号的数字(正号或负号)相加,则当且仅当结果具有相反的符号时,才会发生溢出.

Overflow rule : If two numbers with the same sign (both positive or both negative) are added, then overflow occurs if and only if the result has the opposite sign.

但是在示例之后,您对问题的正文提出了不同的看法.

But you ask something different on the body of your question after your example.

所以这里的结果有左第五位10001-它溢出了吗?

不!这里没有溢出.第五位是进位/借位.如果您在谈论加法,请随身携带.如果您正在谈论减法,请借用.

No! there is no overflow here. That fifth bit is the carry/borrow. Carry if you are talking about addition. Borrow if you are talking about subtraction.

当您试图代表的数字超出了可以代表的数字范围时,就会发生溢出.在您的示例中,您使用的是4位二进制补码,这意味着您可以表示-8(1000)到+7(0111)范围内的任何数字.减去2-1的结果是+1,该数字在表示范围内.

Overflow occurs when the number that you trying to represent is out of the range of numbers that can be represented. In your example you are using 4-bits two's complement, that means you can represent any number in the range -8 (1000) up to +7 (0111). The result of your subtraction 2-1 is +1, a number that lies within the range of representation.

当我们添加一个负数和一个正数操作数时,结果将始终在表示范围内.当我们将两个具有相同符号(正号或负号)的数字相加并且结果具有相反的符号时,就会发生溢出.

When we add a negative and a positive operand, the result will always be in the range of representation. Overflows occur when we add two numbers with the same sign (both positive or both negative) and the result has the opposite sign.

大多数误解周围的进位和溢出是因为我们将进位用作生成溢出标志的参数之一.它们是密切相关的,但它们不是一回事.

Most of misunderstanding surrounding carry-out and overflow comes from the fact we use the carry-out as one of the parameters to generate overflow flag. They are strongly related but they are not the same thing.

在以2的补码加数字时,如果进位和进位到最高有效位(符号位)不同,则表示发生了溢出.

When adding numbers in two's complement, if the carry-out and the carry-on into the most significant bit (sign bit) are different that means an overflow has occurred.

让我们看两个带有负结果的负操作数:

Let's see two negative operands with a positive result:

-8 + (-1) = -9 

 1000  (carry)
  1000 (-8)
+ 1111 (-1)
------
  0111 (+7) OVERFLOW!

进位值为1,进位符号位(MSB)为0.

The carry-out is 1 and the carry-on to sign bit (MSB) is 0.

现在,以两个带有负结果的正操作数为例.

And now, an example of two positive operands with a negative result.

+7 + 1 = +8

 0111  (carry)
  0111 (+7)
+ 0001 (+1)
------
  1000 (-8) OVERFLOW!

进位输出为0,进位到符号位(MSB)为1.

The carry-out is 0 and the carry-on to sign bit (MSB) is 1.

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

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