-1

So I am trying to return the exponent value for f. If x is infinity or NaN I need to return Tmax. f is to be interpreted as the bit-level representations of single-precision floating point values and yes I mean to pass in an unsigned variable. I know I need to remove the appropriate value for the bias. I just cant figure out how. I can use any operations I want but only 8 of them (= does not count).

int float_exp(unsigned uf) {
    int mask = 2139095040;
    int hold;
    hold = uf & mask;
    if(mask == hold){
            return Tmax;
    } else {
        return ;// something here???
    }

}

I cant figure out how to get the exponent. Any help would be appreciated!

Shaw
  • 169
  • 1
  • 3
  • 12
  • 3
    If it wasn't for homework, you'd look up `frexp()` in your manual. – Jonathan Leffler Mar 03 '14 at 23:38
  • See also [Dividing floating point numbers by half in binary](http://stackoverflow.com/questions/22159484/dividing-floating-point-numbers-by-half-in-binary). Not a direct duplicate, but closely related. – Jonathan Leffler Mar 03 '14 at 23:43
  • @JonathanLeffler your comments are not helping. – Shaw Mar 03 '14 at 23:45
  • 2
    Why are you expressing a mask in decimal? Hex would make what you're doing much clearer, to your readers and to yourself. – keshlam Mar 03 '14 at 23:49
  • 1
    bitwise operations should use hexadecimal masks since it relate directly to the binary encodings – phuclv Mar 03 '14 at 23:54

2 Answers2

2

Check the floating point format http://en.wikipedia.org/wiki/IEEE_754-1985

The binary exponent is in bits 23 through 30. So after masking against 0x7F800000, as you have done, you just need to shift what's left to the right by 23 bits and you have your binary exponent.

Then adjust for the bias, by subtracting it off.

sj0h
  • 902
  • 4
  • 4
1

If the number is denormalized subtract 127 from the exponent. If it is not then subtract 126.

Cole
  • 116
  • 7