Initial situation/ task
I want to reinterpret the double
-precision floating point number to a long
integer number. The byte size of both data types is equal on my platform (i.e. 8 bytes). I do not want to touch the bit pattern, i.e. it is important to keep the pattern and treat the pattern as if it was the other data type.
What I have tried so far
I have done the following:
/* Cast the data to an integral type [long], so floats and doubles can be read as well */
double double_val;
double* double_ptr;
long* long_ptr;
long long_val;
double_val = out1[it]; //23.5f;
double_ptr = &double_val; // set address of new memory location with double-precision number
long_ptr = (long*)double_ptr; // cast float pointer to uint64_t pointer
long_val = *long_ptr;
Question
C++ has the reinterpret_cast
. Does my code perform something equal?
What I do is to cast the pointer, but I use two memory locations. Is there a shorter, more convenient way to accomplish what I want?
Are there any potential side effects of my code?
"Why would you want to do this?"
I have to trick my automatic debug scripting. The debugging API can only read variables of integral type (long
values).
Another reason is that I want to print
the bit pattern at the memory location of that variable. For integral types this can be done with the format specifier 0x%08X
. I am not sure if this would be applicable for a memory location with a variable of type double
directly as well, or if this is implementation specific or would cause any other side effects.