简体   繁体   English

C- 为什么 char c=129 会转换成 -127?

[英]C- why char c=129 will convert into -127?

if we assign +128 to char variable then it is converted into -128 because of binary equivalent( 10000000-first bit tells sign ).如果我们将+128分配给 char 变量,那么由于二进制等效( 10000000-first bit tells sign ),它将被转换为-128 Binary equivalent of 129 is 10000001 , What will be the value it will be converted to? 129二进制等价物是10000001 ,它将转换为什么值?

char c=129;字符 c=129;

Thanks, S谢谢,S

There are actually several possibilities.其实有几种可能。

If char is a signed type, then如果char是有符号类型,则

char c = 129;

implicitly converts the int value to char .int值隐式转换为char The result is implementation-defined (or it can raise an implementation-defined signal, but I don't know of any implementations that do that).结果是实现定义的(或者它可以引发实现定义的信号,但我不知道任何实现)。 In most implementations, the conversion yields - 127 because signed types are represented using two's-complement.在大多数实现中,转换产生 - 127因为有符号类型使用二进制补码表示。 In an unsigned 8-bit type, 10000001 represents the value 129 ;在无符号 8 位类型中, 10000001表示值129 in a signed 8-bit type, the same bit pattern represents -127 ;在有符号的8 位类型中,相同的位模式表示-127 the conversion isn't required to keep the same bit pattern, but it very commonly does.转换不需要保持相同的位模式,但它很常见。

If plain char is an unsigned type (as it is on some systems), then the value 129 is within the range of char , and the conversion simply yields the value 129 .如果普通char是无符号类型(就像在某些系统上一样),则值129位于char的范围内,并且转换只会产生值129

But all this assumes that char is 8 bits.但所有这些都假设char是 8 位。 The C standard requires CHAR_BIT (the number of bits in a byte, or equivalently in a char object) to be at least 8, but it permits it to be larger. C 标准要求CHAR_BIT (字节中的位数,或等效于char对象中的位数)至少为8,但它允许它更大。 You're not likely to run into a system with CHAR_BIT > 8 , but it's not uncommon in C implementations for DSPs .您不太可能遇到CHAR_BIT > 8的系统,但这在DSP 的C 实现中并不少见。

Depending on all this is rarely a good idea.依赖所有这些很少是一个好主意。 Whatever it is you're trying to accomplish by assigning 129 to a char object, there's very likely a better, clearer, and more portable way to do it.无论您试图通过将 129 分配给char对象来完成什么,很可能有一种更好、更清晰、更便携的方法来做到这一点。

Negative numbers are stored as 2's compliment of the positive of that number.负数存储为该数正数的 2 的补码。

For eg:例如:

  -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.

Similarly, 130 will be assigned as -126.同样,130 将被分配为 -126。

Thanks!!谢谢!!

Attempt to store +129 in a char results in storing -127 in it.尝试在字符中存储 +129 会导致在其中存储 -127。 When we attempt to store +128 in a char the first number on the negative side, ie -128 gets stored.当我们尝试在 char 中存储 +128 时,负侧的第一个数字,即 -128 被存储。 This is because from the 9-bit binary of +128, 010000000, only the right-most 8 bits get stored.这是因为从 +128, 010000000 的 9 位二进制中,只有最右边的 8 位被存储。 But when 10000000 is stored the left-most bit is 1 and it is treated as a sign bit.但是当存储 10000000 时,最左边的位是 1,它被视为符号位。 Thus the value of the number becomes -128 since it is indeed the binary (2's complement) of -128.In general, if we exceed the range from positive side we end up on the negative side.因此,数字的值变为 -128,因为它确实是 -128 的二进制(2 的补码)。通常,如果我们超出正侧的范围,我们最终会处于负侧。 Vice versa is also true.反之亦然。 If we exceed the range from negative side we end upon positive side.如果我们超出了负侧的范围,我们就会以正侧结束。

When numbers are in the negative domain (ie signing bit is 1 ) they assume minimum value when the "actual" binary representation (ie ignoring signing bit, only dealing with the part that stores the number) is 0.当数字在负域中时(即签名位为1 ),当“实际”二进制表示(即忽略签名位,仅处理存储数字的部分)为 0 时,它们假定最小值。

Eg例如

The signed binary: 10000000 represents -128 in decimal.带符号的二进制: 10000000表示十进制的 -128。

This makes sense as for every increment of the binary representation there is a increment of the decimal number as well (remember -127 is one more and than -128).这是有道理的,作为二进制表示的每个增量有十进制数的增加,以及(记住-127是一个比-128)。 If you deal only in decimal and ignore any rules that prohibit overflow behavior, you'll see that as the number approaches the maximum positive value it immediately assumes the maximum negative value and counts up to 0.如果您只处理十进制并忽略任何禁止溢出行为的规则,您将看到当数字接近最大正值时,它立即假定最大负值并计数到 0。

Another point you can see is that the binary representation for -1 would be 11111111 .您可以看到的另一点是 -1 的二进制表示是11111111 If we increment this by one we get 1 00000000 where the leading one is discarded, giving us the number 0.如果我们将其加一,我们会得到1 00000000 ,其中前导的一个被丢弃,给我们数字 0。

A quick way to determine what is the value of a signed negative number is to take the binary representation (ignoring the signing bit) and add it to the maximum negative number.确定带符号负数的值的一种快速方法是采用二进制表示(忽略签名位)并将其添加到最大负数。

For eg例如

If we have the binary representation of 10000001 where the unsigned value is 129, we can see that ignoring the signing bit we have 1 which we add to -128 yielding the answer of -127.如果我们有10000001的二进制表示,其中无符号值为 129,我们可以看到忽略签名位我们有1 ,我们将其添加到 -128 产生 -127 的答案。

Please note: I am ignoring the fact that sometimes overflows are not permitted and undefined behavior may result.请注意:我忽略了有时不允许溢出并且可能导致未定义行为的事实。 I'm also dealing with signed chars in this answer.我也在这个答案中处理签名字符。 Should you have a case where you're dealing with unsigned chars then the full binary expression is used to give it it's value (eg 10000001 = 129).如果您遇到处理无符号字符的情况,则使用完整的二进制表达式为其赋值(例如10000001 = 129)。

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

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