float类型数据占4字节。
#include<stdio.h> struct DATA { unsigned char d0:1; unsigned char d1:1; unsigned char d2:1; unsigned char d3:1; unsigned char d4:1; unsigned char d5:1; unsigned char d6:1; unsigned char d7:1; unsigned char d8:1; unsigned char d9:1; unsigned char d10:1; unsigned char d11:1; unsigned char d12:1; unsigned char d13:1; unsigned char d14:1; unsigned char d15:1; unsigned char d16:1; unsigned char d17:1; unsigned char d18:1; unsigned char d19:1; unsigned char d20:1; unsigned char d21:1; unsigned char d22:1; unsigned char d23:1; unsigned char d24:1; unsigned char d25:1; unsigned char d26:1; unsigned char d27:1; unsigned char d28:1; unsigned char d29:1; unsigned char d30:1; unsigned char d31:1; }data; typedef union A { float i; // 存放十进制数 DATA test; } A; void show(float x) { A v; v.i = x; printf("%8g ==== ",x); printf("%d %d%d%d%d%d%d%d%d %d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d ", v.test.d31 , v.test.d30 , v.test.d29 , v.test.d28 , v.test.d27 , v.test.d26 , v.test.d25, v.test.d24 , v.test.d23 , v.test.d22 , v.test.d21 , v.test.d20 , v.test.d19 , v.test.d18 , v.test.d17, v.test.d16 , v.test.d15 , v.test.d14 , v.test.d13 , v.test.d12 , v.test.d11 , v.test.d10 , v.test.d9, v.test.d8 , v.test.d7 , v.test.d6 , v.test.d5 , v.test.d4 , v.test.d3 , v.test.d2 , v.test.d1, v.test.d0 ); } int main() { // show(-128.75f); return 0; }
计算公式:
x=(-1)^S*2^(E-127)*(1.M)
S表示符号位,正或者负。 在计算机中,0表示正,1表示负
E表示指数
M表示小数点后的数
====================接下来是几个实例========================
接下来,我们套公式:
(-1)^S*2^(E-127)*(1.M)
=(-1)^1*2^(134-127)*(1+0.M)
=-( 2^7 + 2^7 * 0.M)
=-(128 + 128 * 3/512)
=-128.75
---------------------------------------------------------------------
S的值为 0
E的值为 0x7B 它的十进制数是 123
M的值为 .1111 1001 1010 0110 1011 011
先计算二进制 111 1100 1101 0011 0101 1011 的值
十六进制是 7 c d 3 5 b
用计算器算出来的结果是:8180571
由此算出:
.1111 1001 1010 0110 1011 011 的值为 8180571/(2^23)
接下来套公式:
(-1)^S*2^(E-127)*(1.M)
= (-1)^0 * 2^(123-127) * (1+0.M)
= 2^-4 + 2^-4 * 8180571*2^-23
=1/16 + 8180571/134217728
=0.0625 + 0.06095000256
=0.12345000256
在此感谢王哥和我一起分析。