本文介绍了用C铸造浮到INT(按位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
或编译运行转换)?
编辑#1:
我有以下功能,但它在某些情况下会失败:
I have to following function but it fails in some cases:
输入:INT X(包含IEEE 754格式的32位单precision号)
Input: int x (contains 32 bit single precision number in IEEE 754 format)
if(x == 0) return x;
unsigned int signBit = 0;
unsigned int absX = (unsigned int)x;
if (x < 0)
{
signBit = 0x80000000u;
absX = (unsigned int)-x;
}
unsigned int exponent = 158;
while ((absX & 0x80000000) == 0)
{
exponent--;
absX <<= 1;
}
unsigned int mantissa = absX >> 8;
unsigned int result = signBit | (exponent << 23) | (mantissa & 0x7fffff);
printf("\nfor x: %x, result: %x",x,result);
return result;
编辑#2:
另外需要的帮助:http://cs.stackexchange.com/questions/3484/converting-function-to-bitwise-only
推荐答案
C有联盟来处理这种类型的视图数据:
C has the "union" to handle this type of view of data:
typedef union {
int i;
float f;
} u;
u u1;
u1.f = 45.6789;
/* now u1.i refers to the int version of the float */
printf("%d",u1.i);
这篇关于用C铸造浮到INT(按位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!