csapp-Lecture1-4
#include "stdio.h" #include "stdlib.h" int main(){ //写入一个整数16 //32位的int表示为:00000000 00000000 00000000 00010000 //用unsigned char去模拟 unsigned char *uc=(unsigned char*)malloc(sizeof(unsigned char)*4); uc[0]=0b00010000; uc[1]=0b00000000; uc[2]=0b00000000; uc[3]=0b00000000; printf("%d\n",*((int*)uc)); //写入一个double //32位的float表示 (-1)**S*1.M*2**E // 正数 S=0 M=0 E=1 这个数字为2 //32 float IEEE编码为 01000000 00000000 00000000 00000000 uc[0]=0b00000000; uc[1]=0b00000000; uc[2]=0b00000000; uc[3]=0b01000000; printf("%f\n",*((float*)uc)); //float表示正无穷, 01111111 10000000 00000000 00000000 uc[0]=0b00000000; uc[1]=0b00000000; uc[2]=0b10000000; uc[3]=0b01111111; printf("%f\n",*((float*)uc)); //float表示NAN 01111111 11111111 00000000 00000000 uc[0]=0b00000000; uc[1]=0b00000000; uc[2]=0b11111111; uc[3]=0b01111111; printf("%f",*((float*)uc)); //浮点数不具有结合律,表现在当一个很大的数去加减一个很小的数的时候 print("%f\n",(3.14+1e10)-1e100); print("%f\n",3.14+(1e10-1e100)); return 0; }