CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    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. #2
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by vincegata View Post
    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. #3
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

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

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    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. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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

Featured