CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Threaded View

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

    Re: signed to unsigned (and vice versa)

    Quote Originally Posted by monarch_dodra
    I'm looking for the exact rules according to standard of casting an integer type of signed type to an unsigned integer of the same rank, as well as the contrary (unsigned to signed).
    Ah, that just means some quoting then
    Quote Originally Posted by C++03 Section 4.7 Paragraphs 1 to 3
    An rvalue of an integer type can be converted to an rvalue of another integer type. An rvalue of an enumeration type can be converted to an rvalue of an integer type.

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

    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.
    Quote Originally Posted by monarch_dodra
    Is it safe to write:
    Code:
    unsigned int max_value = -1;
    On any architecture?
    Yes. In fact, this is pretty much how the standard defines npos for std::basic_string.
    Last edited by laserlight; August 3rd, 2010 at 11:08 AM. Reason: 2n => 2**n, where ** denotes exponentiation
    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

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