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

2019-01-12 01:30发布

问题:

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;

Thanks, S

回答1:

There are actually several possibilities.

If char is a signed type, then

char c = 129;

implicitly converts the int value to 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. In an unsigned 8-bit type, 10000001 represents the value 129; in a signed 8-bit type, the same bit pattern represents -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.

But all this assumes that char is 8 bits. 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. You're not likely to run into a system with CHAR_BIT > 8, but it's not uncommon in C implementations for DSPs.

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.



回答2:

Negative numbers are stored as 2's compliment of the positive of that number.

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.

Thanks!!



回答3:

When numbers are in the negative domain (i.e. signing bit is 1) they assume minimum value when the "actual" binary representation (i.e. ignoring signing bit, only dealing with the part that stores the number) is 0.

E.g.

The signed binary: 10000000 represents -128 in decimal.

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

Another point you can see is that the binary representation for -1 would be 11111111. If we increment this by one we get 1 00000000 where the leading one is discarded, giving us the number 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 e.g

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.

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 (e.g. 10000001 = 129).



回答4:

Attempt to store +129 in a char results in storing -127 in it. When we attempt to store +128 in a char the first number on the negative side, i.e. -128 gets stored. This is because from the 9-bit binary of +128, 010000000, only the right-most 8 bits get stored. But when 10000000 is stored the left-most bit is 1 and it is treated as a sign bit. 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. Vice versa is also true. If we exceed the range from negative side we end upon positive side.



标签: c variables char