【发布时间】:2012-04-29 09:27:09
【问题描述】:
拿走
int x = 5;
float y = x;
//"I know it's a float .. you know it's a float .. but take it's address
// and pretend you're looking at an integer and then dereference it"
printf("%d\n", *(int*)&y); //1084227584
为什么我会看到这个数字?
- 5 在二进制是
0101 - 5 可以认为是
(1.25 * 2^2),也就是说
可以表示为:
[sign bit] - 0
[8 bits worth of exp] - 129 (129-127=2) - 1000|0001
[23 bits of .xxxxxxx] - 25 - 1100|1
放在一起,我有
[sign bit][8 bits worth of exp][23 bits worth of .xxx]
0 10000001 11001000000000 //2126336
请问我错过了什么?
【问题讨论】:
-
浮点表示会因您的架构和环境而异。这段代码的运行环境是什么?
-
hmm .. 在 64 位 mac 上运行
标签: c floating-point number-formatting