I'm tasked to code a function (in C) that converts a float value to an IEEE standard for floating point representation. I think I understand how to do it on paper, but when I'm trying to code it I'm confused with how to store the bit pattern.
So if I have the value 3948.125, which is 111101101100.001 , as I'm multiplying the .125 by 2 until its done, how do I store whether its 0 or 1 as I go along?
0.125 x 2 = 0.25 0
0.25 x 2 = 0.5 0
0.5 x 2 = 1 1
= 0.001
In the end the value has to be returned as an int.
Here is a part of the code for converting the decimal part
while(decimal!=0) {
decimal = decimal * 2;
if (decimal > 1) {
//here i would like to store a 1 somewhere
decimal = decimal - 1;
}
else {
//I would like to store a 0 somewhere
}
}
For another example, an input to the program will be 18.113, and the output should be 18.0937500000. I have to convert a 32 bit float value and output an integer representation that only uses 15 bits, 5 bits for exp and 9 bits for frac.
I understand how to do it on paper. When I'm trying to code it, I'm confused with how I store the bits as I calculate them. The code provided is only for converting the decimal part of the input into binary.