So I'm given
const int mask = 0x7F800000
const int shift = 23
const int bias = 127
int exp (float number) {
unsigned int u = *(unsigned int*)&number;
int field = 0;
Honestly, I'm just confused how to format properly to get what I want. Essentially, client inputs a float and then u is the unsigned int that has the same bit pattern as the float.
I know I need to first mask everything except the exponent variables in a number. So I'd assume it's
field = (u & mask)
That should presumably mask the bit pattern except for the part we want - the exponent.
Then step two would be to shift it by 23, so its not surrounded by all the zeros...this is where I get confused.
field = (u & mask) >> shift ???
See, I'm kind of lost. Maybe that's right. We are shifting by 23 to the right regardless.
Then what's last is to take the bias into account. You want to return the actual exponent, not the bits. And the formula to do that is exponent bits = (exp bit pattern) - bias...So then I try this
field = ((u & mask) >> shift) - bias;
return(field);
And I just can't get the right answer. Firstly, please go easy on my formatting. I'm new to C and this is a beginner project. I know it's not good as is. But, can someone assist me in understanding the formatting to make these steps work? Or help with what I'm missing? I'm just looking for assistance on logic.
Three methods:
then you can use
fu.u
Having unsigned value
u
you can for example: