简体   繁体   中英

Value of char c = 255 converted to int

My question is about a paragraph in Stroustrup's book the C++ programming language, 4th edition. He brings an example of having

char c = 255; // 255 is ‘‘all ones,’ ’ hexadecimal 0xFF
int i = c;

and explanation of how it will be converted on machines where char is either signed or unsigned.

What will be the value of i? Unfortunately, the answer is undefined. On an implementation with 8-bit bytes, the answer depends on the meaning of the ''all ones'' char bit pattern when extended into an int. On a machine where a char is unsigned, the answer is 255. On a machine where a char is signed, the answer is −1.

My question is why it will be -1, doesn't it depends on what representation of binary numbers is used on machine? Wouldn't it be 0(-0) if it uses ones' complement and -1 if two's complement?

Quoting C++03 4.7/3:

If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.

Assuming bytes are 8 bits, this means that in theory you get one of the following:

  • -127 if signed magnitude computer.
  • -0 if one's complement computer.
  • -1 if two's complement computer.

The former two barely exist in the real world.

(In the case char is signed 8 bit type) 255 is not a representable value. Converting an unrepresentable value to a signed type results in an implementation defined value (until C++20). So, Stroustrup is simplifying a bit in this step; the result could be anything in this case as far as the standard is concerned.

But assuming the sign representation is two's complement, it is likely that the value will be congruent with 255 modulo 2 8 (in the future C++20 standard, this becomes a guarantee). The value that is congruent with 255 modulo 2 8 and is representable is -1 .

Wouldn't it be 0(-0) if it uses ones' complement

Probably (until C++20). But ones' complement is archaic and hardly used anymore. So, as I said, it seems Stroustrup seems to have chosen to simplify the explanation and assumed two's complement. Or maybe he had the future standard in mind when he wrote it; Or maybe the standard change was proposed to make his book correct :)

Let's look at article Signed number representations .

Aha!

We can see that there are 5 types of representations mentioned:

  • Unsigned
  • Sign and magnitude
  • Ones' complement
  • Two's complement
  • Offset binary
  • Base −2

(and for curious - more Negative base s can be used too)

Let's look at Comparison table for 4-bit numbers.

Extending to 8-bits of 1 we see that:

  • Unsigned = 255
  • Sign and magnitude = -127
  • Ones' complement = -0
  • Two's complement = -1
  • Excess-127 = 127
  • Base −2 = hm ... I need to think about it

So Stroustrup is right in:

Unfortunately, the answer is undefined. On an implementation with 8-bit bytes, the answer depends on the meaning of the ''all ones'' char bit pattern when extended into an int

But he is not exactly right in:

On a machine where a char is unsigned, the answer is 255. On a machine where a char is signed, the answer is −1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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