Bit representation of a double number

I ran this program which, as expected, will know the bit representation of the float:

float x1=-675.78125;
int *pint1;
pint1=(int *)&x1;


for(int i=0;i<8*sizeof(float);i++)
{

if(*pint1&1)
{
    cout<<1;
    }
else
    cout<<0;
    *pint1>>=1;

}

      

But this doesn't work for double:

double x=-675.78125;
int *pint;
pint=(int *)&x;

for(int i=0;i<8*sizeof(double);i++)
{

    if(*pint&1)
    {
        cout<<1;
        }
    else
        cout<<0;
        *pint>>=1;

    }

      

Could you please explain to me why this is so? how would you do it? Thank you so much for your help.

0


source to share


1 answer


The reason your first program works and the second is not for your specific hardware, the size of a float is the same as an int, while an int doesn't have enough room for all the bits in double

.



But you're already breaking the strict dithering rules, so if you really want to print the bits of a floating point type, the correct way to do it is to push on unsigned char*

and then iterate over each char bit as the pointer increments by every byte of the base floating point type. Also note that in the case of big-vs-little endian, the results of your program may differ.

+3


source







All Articles