简体   繁体   English

减法8位二进制数

[英]Subtraction 8-bit binary number

I'm trying to add up two 8 bit numbers which one is negative and second one is positive. 我试图将两个8位数字加起来,其中一个为负数,第二个为正数。 Here it is what I do: 这是我的工作:

92-113

so I represent each number as binary 所以我用二进制表示每个数字

92 - 01011100
113 - 01110001

after changing 0 to 1 and 1 to 0 I get : 将0更改为1和1更改为0后,我得到:

10001110 and after adding 1 I have 1000111 which is -113

then I'm adding them up and I get : 然后我将它们加起来,得到:

11101011

what totally makes no sense, what I probably do wrongly ? 完全没有道理,我可能做错了什么? Would really love to know where I make mistake as it's really basic knowledge ;/ 我真的很想知道我在哪里犯了错误,因为它确实是基础知识; /

You're not missing anything - 11101011 is the binary equivalent of -21 (92-113) in 8-bit signed binary. 您不会丢失任何东西11101011是8位带符号二进制文件中-21(92-113)的二进制等效项。

For singed integer types, the left=most bit determine if the number is positive or negative. 对于单个整数类型,最左=最上位确定该数字是正数还是负数。 To get the additive inverse, you flip all bits but the rightmost. 要获得加法逆,可以翻转除最右边以外的所有位。 It;s the same process you used to convert 113 to -113. 与您将113转换为-113的过程相同。

Doing that yields 00010101 which is 21. So 11101011 is -21. 这样做将得出00010101 ,即21。因此11101011为-21。

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

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