69,565
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
int main() {
float f;
double d;
char bs[65];
char b[65];
char s[80];
unsigned char *p;
char e[12];
char *t;
int ex;
while (1) {
printf("Input a float point number:");fflush(stdout);
rewind(stdin);
fgets(s,80,stdin);
if (1==sscanf(s,"%f",&f) && 1==sscanf(s,"%lf",&d)) break;
}
printf("f=%g\n",f);
p=(unsigned char *)&f;
printf("hex=%02X %02X %02X %02X\n",p[3],p[2],p[1],p[0]);
ltoa(*(long *)&f,b,2);
sprintf(bs,"%032s",b);
printf("bin=%s\n",bs);
printf("bin=%.1s %.8s %s\n",bs,bs+1,bs+9);
strncpy(e,bs+1,8);e[8]=0;
ex=strtol(e,&t,2);
printf(" %c %-4d-127 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9);
ex-=127;
printf(" %c %-8d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9);
printf("\nd=%lg\n",d);
p=(unsigned char *)&d;
printf("hex=%02X %02X %02X %02X %02X %02X %02X %02X\n",p[7],p[6],p[5],p[4],p[3],p[2],p[1],p[0]);
_i64toa(*(__int64 *)&d,b,2);
sprintf(bs,"%064s",b);
printf("bin=%s\n",bs);
printf("bin=%.1s %.11s %s\n",bs,bs+1,bs+12);
strncpy(e,bs+1,11);e[11]=0;
ex=strtol(e,&t,2);
printf(" %c %-6d-1023 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12);
ex-=1023;
printf(" %c %-11d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12);
return 0;
}
//Input a float point number:0.125
//f=0.125
//hex=3E 00 00 00
//bin=00111110000000000000000000000000
//bin=0 01111100 00000000000000000000000
// + 124 -127 1.00000000000000000000000
// + -3 1.00000000000000000000000
//
//d=0.125
//hex=3F C0 00 00 00 00 00 00
//bin=0011111111000000000000000000000000000000000000000000000000000000
//bin=0 01111111100 0000000000000000000000000000000000000000000000000000
// + 1020 -1023 1.0000000000000000000000000000000000000000000000000000
// + -3 1.0000000000000000000000000000000000000000000000000000
//
Input a float point number:0.0
f=0
hex=00 00 00 00
bin=00000000000000000000000000000000
bin=0 00000000 00000000000000000000000
+ 0 -127 1.00000000000000000000000
+ -127 1.00000000000000000000000
d=0
hex=00 00 00 00 00 00 00 00
bin=0000000000000000000000000000000000000000000000000000000000000000
bin=0 00000000000 0000000000000000000000000000000000000000000000000000
+ 0 -1023 1.0000000000000000000000000000000000000000000000000000
+ -1023 1.0000000000000000000000000000000000000000000000000000