As shown in the code below, I am trying to copy the bits from a long longnum
to two doubles, d1
and d2
, using different methods: pointer-casting + dereferencing and 'bitwise-and'ing respectively.
# include <stdio.h>
int main(void) {
long longnum = 0xDDDDDDDDDDDDDDDD;
double d1 = *((double*)(&longnum));
double d2 = longnum & 0xFFFFFFFFFFFFFFFF;
printf("%ld\n\n",longnum);
printf("%lf\n\n",d1);
printf("%lf\n",d2);
return 0;
}
The issue is that both the doubles are not printed the same way, as shown in the output below.
-2459565876494606883
-1456815990147462891125136942359339382185244158826619267593931664968442323048246672764155341958241671875972237215762610409185128240974392406835200.000000
15987178197214945280.000000
Given the size of DBL_MAX
, the max size of a double, it seems to me that it's the giant number that's actually the sensible output of the two doubles printed.