二的补 [英] two's complement
问题描述
据我所知,补算法中是:
1.Re present小数二进制。结果
2.Inverse所有位。结果
3.加入1到最后的位
块引用>有关
3
的数量,这对其重新presentation是:0000000000000011
二进制补码结果将1111111111111101
是-3
。结果
到现在为止还挺好。
但对于2
,其数目其重新presentation是0000000000000010
二进制补码的结果是1111111111111101
,这是不是2,但-3。结果
我在做什么错了?解决方案0 ... 0010 // 2
1 ... 1101 //翻转位
1 ... 1110 //新增一个它为负过:
1 ... 1110 // -2
0 ... 0001 //翻转位
0 ... 0010 //新增一个As far as I know, the two's complement algo is:
1.Represent the decimal in binary.
2.Inverse all bits.
3.Add 1 to the last bit.For the number
3
, which its representation is:0000000000000011
the result of the two's complement would be1111111111111101
which is-3
.
So far so good. But for the number2
which its representation is0000000000000010
the result of the two's complement would be1111111111111101
, which isn't 2 but -3.
What am I doing wrong?解决方案0...0010 // 2 1...1101 // Flip the bits 1...1110 // Add one
It works for negative too:
1...1110 // -2 0...0001 // Flip the bits 0...0010 // Add one
这篇关于二的补的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!