Possible Duplicate:
How do promotion rules work when the signedness on either side of a binary operator differ?
When casting from an unsigned integer to a signed integer, I know the represention of the variable’s bits changes. For instance, 255 may become -1, when converting from uint8 to int8. However, I was never sure what a ‘cast’ or ‘conversion’ entailed for the underlying bits themselves.
My question is, is the raw bit pattern of an integer variable guaranteed to remain the same after a static_cast between signed and unsigned types, or is it possible that it be transformed by the cast in some way?
Out of curiosity too, does a static_cast between integer signage types generate assembly, or is it used only so the compiler knows what asm instructions to generate?
edit:
Here’s an example of the kind of scenario I would want to know about:
unsigned int uintvar = random();
unsigned int control = uintvar;
assert(control == static_cast<unsigned int>(static_cast<signed int>(uintvar)));
Ignoring the fact the double cast would get optimized away, would this example be guarenteed to always hold true?
The bit pattern doesn’t change at all (on most architectures you’re likely to encounter in practice). The difference is in the instructions generated by the compiler to manipulate the values.