本文介绍了C-为什么char c=129会转换成-127?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我们将 +128
分配给 char 变量,那么它会被转换为 -128
因为二进制等效(10000000-first bit tell sign
).129
的二进制等价物是 10000001
,它将被转换成什么值?
if we assign +128
to char variable then it is converted into -128
because of binary equivalent(10000000-first bit tells sign
).Binary equivalent of 129
is 10000001
, What will be the value it will be converted to?
char c=129;
谢谢,S
推荐答案
负数存储为该数的正数的 2 补码.
Negative numbers are stored as 2's compliment of the positive of that number.
例如:
-127 is stored as 2's compliment of 127
127 is 01111111 in binary
2's compliment=(1's compliment of a number) + 1
therefore, 2's compliment of 127(01111111) is 10000001(-127).
And 10000001 is 129:: Therefore when you give 129 to a char variable, compiler takes it as negative number 127.
同样,130 将被分配为 -126.
Similarly, 130 will be assigned as -126.
谢谢!!
这篇关于C-为什么char c=129会转换成-127?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!