I am working on a homework assignment, where we are supposed to convert an int to float via bitwise operations. The following code works, except it encounters rounding. My function seems to always round down, but in some cases it should round up.
For example 0x80000001 should be represented as 0xcf000000 (exponent 31, mantissa 0), but my function returns 0xceffffff. (exponent 30, mantissa 0xffffff).
I am not sure how to continue to fix these rounding issues. What steps should i take to make this work?
unsigned float_i2f(int x) {
if(x==0) return 0;
int sign = 0;
if(x<0) {
sign = 1<<31;
x = -x;
}
unsigned y = x;
unsigned exp = 31;
while ((y & 0x80000000) == 0)
{
exp--;
y <<= 1;
}
unsigned mantissa = y >> 8;
return sign | ((exp+127) << 23) | (mantissa & 0x7fffff);
}
Possible duplicate of this, but the question is not properly answered.