Reputation: 60381
In C++, does the result of the conversion of a signed integral value, to an unsigned integral value, that can be of two different sizes (ex: short int
to unsigned long long int
, or long long int
to unsigned char
) is well defined by the standard and platform independent (regardless of how signed integer are represented for example)?
Upvotes: 6
Views: 2020
Reputation: 60979
Yes, the value is defined and independent of the representations used. [conv.integral]/2:
If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type).
Obviously, the size of the destination type matters, though; long long
to unsigned char
might yield a different value than long long
to unsigned int
.
Upvotes: 6