# Thread: Extract exponent from a double value

1. I think I came up with a solution. It involves a union, a struct and a bitfield... think about it. 2. Does it have to be ISO C that you are using?

Many compilers define behaviour of unions that might be able to be used - An example of this is XC8 c compiler and it would solve the problem without any arithmetic operators. 3. Originally Posted by GReaper I think I came up with a solution. It involves a union, a struct and a bitfield... think about it.
Ok, that occurred to me too, but how does one take a negative number and "normalize" it by +1022, so that -1022 becomes 0, or -1021 becomes 1?

Unless we're reading the question incorrectly, and that is likely.

The OP indicated unsigned int, offset by +1022.

The question doesn't say unsigned int, it says int.

Fine, so how about the nibble? The exponent is 12 bits.

Unless you have a 4 bit processor (and they did/do exist), how does one shift and sign extend that 12 bit value into a 16 or 32 or 64 bit integer?

I think that's the real question, now that I read both posts.

In other words, frexp extracts the exponent as an integer. Suppose they question says that without even including the library, and therefore not calling frexp, how do we extract those 12 bits without rolling, bit fiddling or all else that is excluded?

Maybe they expect us to do this with inline assembler. That may qualify to the literal text.

If so, it's trivial. 4. x = x +1022 5. Originally Posted by GReaper I think I came up with a solution. It involves a union, a struct and a bitfield... think about it.
OK; I see your solution for this stupid problem.

Tim S. 6. Thank you for yours answers. I know the solutions. In this case I have to use unions and bit fields. I gave me the proper solution. 7. Well....There is a pure mathematical solution.
If you want an expoent, you can use logarithm...

Code:
```int getexp( double d )
{  return floor(log2(fabs(d))); }```
But, of course, you have to add 1023... 8. It turns out that the "add 1022/1023" part is a misunderstanding, the challenge just wants you to extract the exponent part of the double value. 9. It was crewel of them to use implementation defined behaviour as a solution! 10. Originally Posted by Click_here It was crewel of them to use implementation defined behaviour as a solution!
Thank you, I now know what "crewel" means.  Popular pages Recent additions exponent, extract, floating point number, int, unsigned 