I would like to store all bits of a double
in a 64-bit integer in C.
I.e. the most significant bit should be the sign, after that the exponent, then the fraction.
I would like to use a union.
CodePudding user response:
The safest way to do this is to use memcpy
.
double x = 123.456;
uint64_t y;
memcpy(&y, &x, sizeof y);
If you don't have memcpy
at your disposal, you can use a union
:
union {
double d;
uint64_t i;
} u;
double x = 123.456;
uint64_t y;
u.d = x;
y = u.i;