C:如何将双数(例如123.45)存储在浮点变量或双变量或长双变量中?

Sud*_*Roy 3 c

#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)

Mar*_*nen 9

请参阅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