#include <stdio.h>
int main () {
float a = 123.0;
unsigned char ch, *cp;
ch = 0xff;
cp = (unsigned char *) &a;
printf ("%02x", ch&(*(cp+3)));
printf ("%02x", ch&(*(cp+2)));
printf ("%02x", ch&(*(cp+1)));
printf ("%02x\n", ch&(*(cp+0)));
/*I have written this program to see binary representation, but I can not understand the output, the binary representation?
}
Run Code Online (Sandbox Code Playgroud)
请参阅Wikipedia:http://en.wikipedia.org/wiki/Single_precision_floating-point_format,它描述单精度浮点(典型的C float,但取决于编译器)为1位符号,8位偏置指数,以及24位尾数(存储23位).
对于你的例子:
123.0 = 42f60000 十六进制 = 0 10000101 11101100000000000000000 箱
1位符号= 0(表示正数)
8位偏置指数= 10000101 仓 = 133 癸 - 127 癸 = 6 癸
23位的尾数= 11101100000000000000000 仓 = 1.111011 仓(注暗示领先1)转换1.111011 bin x 2 6 dec = 1111011.0 bin = 123.0 dec