I\'m trying to convert an int
into a custom float, in which the user specifies the amount of bits reserved for the exp and mantissa, but I don\'t understand how
To answer a comment posted on 'how to do this in code': (Assuming it's an IEEE float)
A) Extract an unsigned 'exponent' and 'mantissa' from the IEEE float.
i) exp = 0x7F800000 & yourFloatVar;
//this takes bit b1 through b8 from the float. (b0 is the signed bit, b9 and on is the mantissa)
ii) exp = exp >> 23;
//shift right so this exponent is right-oriented
iii) exp += 127;
//add in the bias (127 is for 32-bit only)
iv) mantissa = 0x007FFFFF & yourFloatVar;
//take last 23 bits from float
B) Normalizing
i)
while(true)
{
if( ((mantissa & 0xC0000000) != 0x80000000)
&&((mantissa & 0xC0000000) != 0x40000000) )
{
mantissa = mantissa << 1;
exponent--;
}
else //AKA the float has been normalized
{
break;
}
}
if leading 2 bits aren't '01' or '10' (this is a property of 2's complement - the condition to normalize), then shift over the mantissa and decrement the exponent.
I want to note that this isn't at all the most efficient algorithm for doing this; I just wanted to make the steps clear. Hope I didn't miss anything!