简体   繁体   English

需要一些帮助来理解使用 2 的补码的二进制加法/减法

[英]Need some assistance understanding binary addition/subtraction using 2's complement

If A = 01110011, B = 10010100, how would I add these?如果 A = 01110011,B = 10010100,我将如何添加这些?

I did this:我这样做了: 在此处输入图像描述

ie: 01110011 + 10010100 = 100000111即:01110011 + 10010100 = 100000111

Though, isn't it essentially 115 + (-108) = 7, whereas, I'm getting -249虽然,它本质上不是 115 + (-108) = 7,而我得到的是 -249

Edit: I see that removing the highest order bit (overflow) I get 7 which is what I'm looking for but I'm not getting why you wouldn't have the extra bit.编辑:我看到删除最高位(溢出)我得到 7 这是我正在寻找的,但我不明白为什么你不会有额外的位。

Edit**: Ok, I figured it out.编辑**:好的,我想通了。 There was no overflow as I had assumed there was because 7 is within [-128, 127] (8-bits).正如我所假设的那样,没有溢出,因为 7 在 [-128, 127](8 位)内。 Instead, like Omar hinted at I was supposed to drop the "extra" 1 from addition.相反,就像奥马尔暗示的那样,我应该从加法中删除“额外的”1。

Your calculation is correct and the result is correct.你的计算是正确的,结果是正确的。

You stated that the second number is -108, so both your numbers are interpreted as signed 8-bit values.您说第二个数字是 -108,因此您的两个数字都被解释为有符号的 8 位值。 Thus, you should also interpret your result as an 8-bit signed value, this is why the 9th bit must be dropped, and so the result is 7 (00000111).因此,您还应该将结果解释为 8 位有符号值,这就是必须删除第 9 位的原因,因此结果为 7 (00000111)。

On a real hardware, like an 8-bit CPU for example, as all the registers are 8-bit wide, you are only be able to store the lowest 8-bit of the result, which here is 7 (00000111).在真正的硬件上,例如 8 位 CPU,由于所有寄存器都是 8 位宽,您只能存储结果的最低 8 位,这里是 7 (00000111)。 In some cases, the 9th bit may also be put inside a carry/overflow flag so it's not completely "dropped".在某些情况下,第 9 位也可能放在进位/溢出标志内,因此它不会完全“丢弃”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM