[RESOLVED] unsigned char c = -1; // c has value 255, why is that?
Hello,
So, the
unsigned char c = -1; // c has value 255
Why is that, on a bit level? Negative sign would set the highest bit to 1, and 1 would set the lowest bit to 1: 10000001 which is 129.
Thanks.
Re: unsigned char c = -1; // c has value 255, why is that?
Quote:
Originally Posted by
vincegata
Hello,
So, the
unsigned char c = -1; // c has value 255
Why is that, on a bit level? Negative sign would set the highest bit to 1, and 1 would set the lowest bit to 1: 10000001 which is 129.
Thanks.
Your description is not the way that negative numbers are represented on most systems.
Look up two's complement.
Regards,
Paul McKenzie
Re: unsigned char c = -1; // c has value 255, why is that?
Re: [RESOLVED] unsigned char c = -1; // c has value 255, why is that?
Quote:
Originally Posted by Paul McKenzie
Your description is not the way that negative numbers are represented on most systems.
Look up two's complement.
Actually, Paul McKenzie's tip is not relevant here: two's complement does not come into play because the destination type is an unsigned integer type, whereas two's complement is one of the integer representation systems used for signed integer types. Rather, this conversion rule is relevant:
Quote:
Originally Posted by C++11 Clause 4.7 - Integral conversions
If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2**n where n is the number of bits used to represent the unsigned type). [ Note: In a two's complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). — end note ]
Note that I substituted the superscript in the original text with ** to denote exponentiation. So, in your question, CHAR_BIT is 8 (which is usually the case), and 2**8 = 256. The source integer is -1. Hence, the resulting value is 255, since 255 is congruent to -1 (mod 256). Notice that this would be true regardless of the signed integer representation system.
Re: [RESOLVED] unsigned char c = -1; // c has value 255, why is that?
I know how it's calculated: e.g. (-1 modulo 256) + 256 = 255, I am wondering about the internals.
Btw, "C++11 Clause 4.7 - Integral conversions" is it from the C++ standard? Where can I get?
Re: [RESOLVED] unsigned char c = -1; // c has value 255, why is that?
Quote:
Originally Posted by vincegata
I know how it's calculated: e.g. (-1 modulo 256) + 256 = 255, I am wondering about the internals.
The internals is that whatever is the bit pattern of -1, the result will have the bit pattern of 255U for unsigned char. (Then again, since the compiler can determine the value to be stored at compile time, there is no need for a run time conversion to begin with.)
Quote:
Originally Posted by vincegata
Btw, "C++11 Clause 4.7 - Integral conversions" is it from the C++ standard?
Yes, C++11 refers to the 2011 edition of the C++ Standard.
Quote:
Originally Posted by vincegata
Where can I get?
You can obtain a PDF copy from the ANSI online store, at a price. Free draft versions are available online.
Re: [RESOLVED] unsigned char c = -1; // c has value 255, why is that?
Re: [RESOLVED] unsigned char c = -1; // c has value 255, why is that?
If you use -Wall, you should get a warning when losing precision like that.
Re: [RESOLVED] unsigned char c = -1; // c has value 255, why is that?
I do use -Wall but for release code only, thank you for a tip.