我正在尝试使用 C 中的位运算来舍入浮点数。我首先将浮点数转换为无符号整数。我认为我的策略应该是获取指数,然后将位归零,但我不确定如何编码。这是我到目前为止所拥有的:
float roundDown(float f);
unsigned int notRounded = *(unsigned int *)&f;
unsigned int copy = notRounded;
int exponent = (copy >> 23) & 0xff;
int fractional = 127 + 23 - exponent;
if(fractional > 0){
//not sure how to zero out the bits.
//Also don't know how to deal with the signed part.