use*_*925 4 c++ pointers casting memory-address
有什么区别
int i = 123;
int k;
k = *(int *) &i;
cout << k << endl; //Output: 123
Run Code Online (Sandbox Code Playgroud)
和
int i = 123;
int k;
k = i;
cout << k << endl; //Output: 123
Run Code Online (Sandbox Code Playgroud)
它们都提供相同的输出但是有什么区别吗?
(我在Fast Inverse Square Root的Quake3代码中找到了第一个片段)
在第三季度:
float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
Run Code Online (Sandbox Code Playgroud)
据我了解,您对以下行感兴趣:
i = * ( long * ) &y;
Run Code Online (Sandbox Code Playgroud)
的y是float,和i是long.因此,将浮点位模式重新解释为整数位模式.