C++类型转换

来源:互联网 发布:福建广电网络电视客服 编辑:程序博客网 时间:2024/05/17 04:20
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; // 输出什么?



(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

所以前面一个是false,后面一个是true。

0 0
原创粉丝点击