Possible Duplicate:
Signed to unsigned conversion in C – is it always safe?
How can uint64_t store a negative number? – isn’t it meant to be unsigned or is my code dangerous and it’s working by fluke?
In the code I assign a negative number to uint64_t uint64_neg ( i expect the negativeness, the sign to be lost) then assign the uint64_t to a int64_t but the negativeness is still there – why does this work please ?
uint64_t uint64_neg = -150;
int64_t int64_neg = uint64_neg;
Thanks a lot.
Section 4.7 paragraph 3 in the standard says (with my emphasis added):
That is, on your compiler, an unsigned to signed conversion just “copies the bits”, leaving some implementation defined value in that unsigned int. This overflows the value of the signed int when copying back over, which happens to be implementation defined on your platform to go back to the same signed number.
The standard does not guarantee any of that behavior.