I think I came up with a solution. It involves a union, a struct and a bitfield... think about it.
I think I came up with a solution. It involves a union, a struct and a bitfield... think about it.
Devoted my life to programming...
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.
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.
Last edited by Niccolo; 06-15-2019 at 09:39 PM.
x = x +1022
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
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.
Well....There is a pure mathematical solution.
If you want an expoent, you can use logarithm...
But, of course, you have to add 1023...Code:int getexp( double d ) { return floor(log2(fabs(d))); }
Last edited by flp1969; 06-16-2019 at 07:07 AM.
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.
Devoted my life to programming...
It was crewel of them to use implementation defined behaviour as a solution!