简体   繁体   English

使用2的补码将两个负数相加

[英]Adding two Negative Numbers using 2's Complement

I was wondering if someone could double check my work for me real quick. 我想知道是否有人可以为我快速地仔细检查我的工作。 If I'm given two negative numbers: -33 and -31. 如果给我两个负数:-33和-31。 If I add them together what will be the result using 2's complement. 如果将它们加在一起,使用2的补码将是什么结果。

NOTE: A word length of 6-bits MUST being used for the operation. 注意:该操作必须使用6位的字长。


MY ANSWER 我的答案

So after doing this I computed -31 to be 100001 in 2's. 因此,执行此操作后,我计算出-31为2的100001。 I also computed -33 to be 011111 in 2's complement. 我还计算了-33为2的补码中的011111。 When adding them together I got 1000000, however this number is 7 digits so I chopped off the higher order bit since I'm bound to a word length of 6-bits. 将它们加在一起时,我得到了1000000,但是这个数字是7位数字,所以我砍掉了高位,因为我绑定到6位的字长。 This yields the number 000000. Which contains a sign bit of 0, meaning it would be even. 这将产生数字000000。其中包含0的符号位,表示为偶数。 However since the sum of 2 negative's cannot be even it's obviously an overflow. 但是,由于2个负数之和不可能是偶数,因此显然是溢出。 So I take the 2's of 000000 which is simply 000000. 所以我取000000的2,即000000。

So the answer should be: 0 since a buffer overflow occurred. 因此答案应该是:0,因为发生了缓冲区溢出。 Does this seem right to you guys? 你们觉得这对吗? THANKS. 谢谢。 :) :)

First of all: -33 + (-31) cannot be 0. 首先:-33 +(-31)不能为0。

-33 is not representable in 6bit 2's complement. -33在6位2的补码中不能表示。 01 1111b is +31 in decimal, so the addition results in 0. 01 1111b为十进制+31 ,因此加法为0。

So the correct answer is something like that: There is no result because -33 is an invalid number in 6bit representation. 所以正确的答案是这样的:没有结果,因为-33是6位表示形式的无效数字。

in 7 bit 2's complement -33 = 101 1111b 在7位2的补码中-33 = 101 1111b

 110 0001
+101 1111
    =
1100 0000

which is equal to -64. 等于-64。

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

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