二的补 [英] two's complement

查看:151
本文介绍了二的补的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,补算法中是:


  

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 be 1111111111111101 which is -3.
So far so good. But for the number 2 which its representation is 0000000000000010 the result of the two's complement would be 1111111111111101, 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屋!

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