bit representation of a double number

前端 未结 1 401
春和景丽
春和景丽 2021-01-26 03:53

I did this program, which functions as expected, to know the bit representation of a float:

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


for(int i=0;         


        
相关标签:
1条回答
  • 2021-01-26 04:18

    The reason that your first program seems to work and your second doesn't is that for your particular hardware, the size of a float is the same as int, while an int doesn't have enough room for all the bits in a double.

    But you're already violating the strict aliasing rules, so if you really want to print the bits of a floating point type the right way to do it is to cast to unsigned char* and then iterate over each bit of the char while incrementing the pointer over each byte of the underlying floating point type. Also note that on big-vs-little endian the results of your program may vary.

    0 讨论(0)
提交回复
热议问题