二进制补码 [英] two's complement

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

问题描述

据我所知,补码算法是:

As far as I know, the two's complement algo is:

1.用二进制表示十进制.
2.反转所有位.
3.最后一位加1.

1.Represent the decimal in binary.
2.Inverse all bits.
3.Add 1 to the last bit.

对于数字 3,其表示形式为:0000000000000011 二进制补码的结果将是 1111111111111101,即 -3.
到现在为止还挺好.但是对于表示为 0000000000000010 的数字 2,二进制补码的结果将是 1111111111111101,它不是 2 而是 -3.
我做错了什么?

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

它也适用于负面:

1...1110 // -2
0...0001 // Flip the bits
0...0010 // Add one

这篇关于二进制补码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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