#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(int argc,char **argv){
float f=-3.14;
unsigned char *const pf=(unsigned char*)&f,*p;
unsigned char table[4];
unsigned i;//cycle variable
unsigned whole_value;
unsigned sym_value;//symbol bit
unsigned exp_value;//exponent
unsigned mat_value;//mantissa
//show all bytes of f
printf("the value of variable f is: %g\t\n",f);
printf("all bytes of variable f are(from high byte to low byte):\n");
for(p=pf+3;p>=pf;printf("0x%0x\t",*p--));
printf("\012");
//save the above variables to array(low byte first)
for(p=pf,i=0;i<4;table[i++]=*p++);
//anti solution the variable of f(solution 1)
sym_value=table[3] >> 7;
exp_value=((table[3] & 0x7F) << 1) + (table[2] >> 7);
mat_value=((table[2] & 0x7F) << 16) + (table[1] << 8) + table[0];
printf("the value of solution 1:%g\n",pow(-1,sym_value)*(1+mat_value*pow(2,-23))*pow(2,exp_value-127));
//anti solution the variable of f(solution 2)
whole_value=(table[3] << 24) + (table[2] << 16) + (table[1] << 8) + table[0];
sym_value=whole_value >> 31;
exp_value=whole_value << 1 >> 24;
mat_value=whole_value << 9 >> 9;
printf("the value of solution 2:%g\n",pow(-1,sym_value)*(1+mat_value*pow(2,-23))*pow(2,exp_value-127));
return EXIT_SUCCESS;
}
C语言不用指针只用数组和移位操作将二进制转化为十进制IEEE32浮点数
最新推荐文章于 2022-11-24 17:01:58 发布