[RESOLVED] unsigned char c = -1; // c has value 255, why is that?
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: [RESOLVED] unsigned char c = -1; // c has value 255, why is that?

1. Member
Join Date
Jan 2012
Location
USA
Posts
91

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

2. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: unsigned char c = -1; // c has value 255, why is that?

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

3. Member
Join Date
Jan 2012
Location
USA
Posts
91

## Re: unsigned char c = -1; // c has value 255, why is that?

Thanks.

4. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,734

## Re: [RESOLVED] unsigned char c = -1; // c has value 255, why is that?

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

5. Member
Join Date
Jan 2012
Location
USA
Posts
91

## 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?

6. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,734

## Re: [RESOLVED] unsigned char c = -1; // c has value 255, why is that?

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

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.

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.

7. Member
Join Date
Jan 2012
Location
USA
Posts
91

## Re: [RESOLVED] unsigned char c = -1; // c has value 255, why is that?

Ok, I see, thank you.

8. Senior Member
Join Date
Jan 2009
Posts
1,689

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

9. Member
Join Date
Jan 2012
Location
USA
Posts
91

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width