补码加法问题
我正在研究二进制补码加法。基本上我需要显示 -27 到 +31 的加法,这两个数字都使用 6 位二进制。
我的问题是进位操作。也许我做得不对或者什么。
-27 为二进制:111011
+31 是二进制的:011111
我认为答案应该是:
+4 是二进制的: 000010
这就是我正在做的:
Carry 1 1 1 1 1
- 27 1 1 1 0 1 1
+ 31 0 1 1 1 1 1
-------------------------
Sum: 0 1 1 0 1 0
在我看来,计算结果是 52 而不是 4。
我做错了什么?
I'm working on Two's complement addition. Basically I need to show the addition of -27 to +31, with both numbers in binary using 6 bits.
My problem is with carry operations. Maybe I'm not doing it right or something.
-27 is in binary: 111011
+31 is in binary: 011111
The answer I think should be:
+4 is in binary: 000010
Here's what I'm doing:
Carry 1 1 1 1 1
- 27 1 1 1 0 1 1
+ 31 0 1 1 1 1 1
-------------------------
Sum: 0 1 1 0 1 0
Which in my mind computes to 52 not 4.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的数学错了。
27 是二进制的
0 1 1 0 1 1
(注意我为符号添加的前导 0)-27 是二进制补码的
1 0 0 0 1 1
。当你用这个进行数学计算时,你应该得到正确的结果。
这是快速执行二进制补码的“技巧”。
从 LSB 开始,精确地复制数字,直到遇到第一个零,然后也复制该零。之后,翻转所有位,直到 MSB。
这相当于翻转所有位(补码)并加一(将其变成补码),但只需一步即可完成。
your math is wrong.
27 is
0 1 1 0 1 1
in binary (note the leading 0 I added for the sign)-27 is
1 0 0 0 1 1
in two's complement.when you do the math with this, you should get the correct result.
Here's a "trick" to perform two's complement quickly.
Starting from the LSB, copy down the numbers exactly until you encounter the 1st zero, then copy that zero as well. After that, flip all the bits until the MSB.
This is equivalent to flipping all the bits (one's complement) and adding one (to turn it into two's complement), but just in one step.
嗯,
31 - 27 = 4
,正确! :)4 是二进制的 100
,但你确定-27 是 111011
吗?Hmmm,
31 - 27 = 4
, correct! :)4 is 100
in binary though, an are you sure that-27 is 111011
???