IEEE - 754 - find signbit, exponent, frac, normalized, etc
c, c++, ieee-754
Solution
For a single precision number the high bit is the sign, the next 8 bits are the exponent and the remaining 23 bits are the mantissa. So...
bool negative = !!(HexNumber & 0x80000000);
int exponent = (HexNumber & 0x7f800000) >> 23;
int mantissa = (HexNumber & 0x007FFFFF);
If the exponent is 255, the number is either +- infinity or NaN depending on whether the mantissa is zero (0 means infinity). If the exponent is zero then ether the number is +- zero (if the mantissa is zero) or the mantissa is the actual unnormalized fractional value.
If the exponent is anything else, there is a hidden one bit on the top of the fraction, making it 24 bits. In this case, the actual exponent can be calculated by subtracting 127 from the exponent so that it is in the range -127 to +127, the power of two exponent.
Problem
I am taking in a 8 digit hexadecimal number as an IEEE 754 bit floating point number and i want to print information about that number( signbit, expbits, fractbits, normalized, denormalized, infinity, zero, NAN) floating point should be a single. I read up on bit shifting, and i think this is how i am suppose to do it?. however, i am not 100% sure. I understand that the sign bit is found in the left most position of the number. which indicates positive or negative. How much do i shift it to find each? do i just keep shifting it to find each one? Can someone explain how i am to find each one? would i shift by 1 to find the signbit? would i shift by 8 to get the exponent? would i shift by 23 to get the frac? signbit should be zero expbits should be 128 fracbits should be 0x00000000 I think... if so how do i test it after i shift? this is what i have so far ``` #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { short wordOrder = 0x0100; int HexNumber; printf("Hex IEEE - 754\n"); if(wordOrder == 0x0100) { printf("\nbyte order: big-endian\n"); } else { printf("byte order: little-endian\n"); } printf("\n>"); scanf("%x", &HexNumber); printf("\n%#x",HexNumber); return 0; } ``` my input(scanf) on how i want it.. ``` >40000000 0x40000000 ``` which is what its doing..