|
-
August 3rd, 2010, 11:05 AM
#2
Re: signed to unsigned (and vice versa)
 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 
 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.
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|