I have a value like this:
int64_t s_val = SOME_SIGNED_VALUE;
How can I get a
uint64_t u_val
that has exactly
I agree static_cast is appropriate in this case, but no one has mentioned a very similar looking case where static_cast wouldn't preserve bits as might be expected.
char x = -1; // 255
unsigned int x2 = static_cast(x); // 4294967295
unsigned int x3 = static_cast(static_cast(x)); // 255
Watch out for sign extension when you are casting from a small signed value to a large unsigned value. Possibly other combinations are vulnerable too - I haven't thought it all the way through.