Floating Point Bit Level Arithmetic
Hello, for a CS lab I am supposed to take two floating point numbers some operation to perform on them as input, and then perform the operation between the two numbers at the bit level. This means initially, I have to extract the separate parts of each floating point number and store them as integers, like the exponent, sign bit, and fraction part.
I seem to be getting a bit of unexpected behavior when doing this, however.
That is the code that I am using to try and extract and print the separate parts of the floating point number. However, I am getting approximate fraction parts rather than exact numbers in odd cases. For example, inputting 1.5 would give me a fraction part of .4194304. I may simply be misunderstanding IEEE Floating point numbers, but I did not think that 1.5 would have to be rounded... If this is just normal floating point round off error, then all's well, I suppose. I'm just not sure.
#define SIGNBIT ((uint)0x80000000)
#define EXPONENT ((uint)0x7F800000)
#define FRACTION ((uint)0x007FFFFF)
#define BIT32 ((uint)0x80000000)
#define BIT24 ((uint)0x00800000)
typedef unsigned int uint;
float doComp(uint *xf1, uint *xf2, char op)
// 1: extract and display sign, biased and
// unbiased exponent, plus fraction bit parts
uint SIGN1 = (*xf1 & SIGNBIT) >> 31;
uint SIGN2 = (*xf2 & SIGNBIT) >> 31;
uint EXP1 = ((*xf1 & EXPONENT) >> 23) - 127;
uint EXP2 = ((*xf2 & EXPONENT) >> 23) - 127;
uint FRAC1 = (*xf1 & FRACTION);
uint FRAC2 = (*xf2 & FRACTION);
printf("xf1: %lf = %c 1.%u * 2^(%u)\n", *(float*)xf1, SIGN1?'-':'+', FRAC1, EXP1);
printf("xf2: %lf = %c 1.%u * 2^(%u)\n", *(float*)xf2, SIGN2?'-':'+', FRAC2, EXP2);
// 2: compute f1 op f2 at the bit level by
// appropriately shifting and manipulating
// the bit components -- normalize result
Also, another question I have regards a part of performing the operations. For adding and subtracting, I will obviously have to compare the exponents of the two numbers and change one so that they have the same exponent. Then I also have to shift the fraction part of the number. The problem is, how do I deal with the implied one in the fraction part when shifting? Is there an easy way to add it in? I have a few ideas I'm gonna try tonight but any help/hints/prods in the right direction would be appreciated!