#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
float a = 1.0f;
cout << (int)a << endl;
cout << (int&)a << endl;
cout << boolalpha << ( (int)a == (int&)a ) << endl; // 输出什么?
float b = 0.0f;
cout << (int)b << endl;
cout << (int&)b << endl;
cout << boolalpha << ( (int)b == (int&)b ) << endl; // 输出什么?
}
头文件:
#include <iostream>
using namespace std;
功能:
把bool值显示为true或false
( int & )a == static_cast < int& >( a )
( int ) & a == reinterpret_cast < int >( & a );
( int & ) a 不经过转换, 直接得到a在内存单元的值,并将其转换成整数输出。
( int )a a在内存中的值转换成int类型
float类型在内存中存储的形式是 ,符号位 指数 尾数
由754标准:阶码采用增码(该数补码的反符号),尾数采用原码
所以1.0f 在内存中的形式为
0011 1111 1000 0000 0000 0000 0000 0000
所以输出的是 0x3f800000
0 在内存中的的存储形式
0000 0000 0000 0000 0000 0000 0000 0000
所以输出的是0x00000000

本文探讨了浮点数(如1.0f)在计算机内存中的存储形式及如何通过比较其地址和值来理解它们在内存中的位置。通过详细解析不同情况下的输出结果,展示了浮点数与整数之间的内存表示差异。
810

被折叠的 条评论
为什么被折叠?



