> 8-bits for Exponent: Shouldn't the valid range be from -128 to +127?
pow(2,127) is about the same as pow(10,37)
> 24-bits for Mantissa: Does it mean the valid values are from -8388608 to +8388607?
23 bits, and it represents a fraction, not a whole number.
> In my example code, say a user inputs 5000 for salary.
> Would it be correct to say that the Mantissa would be 5000 and the Exponent would be 0?
The mantissa represents a normalised value between [1.0,2.0), and raised to some power of two (the exponent). You can see from the program below that powers of two have all the mantissa bits zero, and increasing by a power of two just increments the exponent. Note that the exponent is biased so that exponents don't need to store an explicit sign bit (exponents of 00 and FF are reserved for things like infinity)
Code:
#include<stdio.h>
int main ( ) {
float arr[] = { 0.125, 0.25, 0.5, 1, 2, 4, 44 };
union {
float f;
unsigned long d;
} a;
int i;
for ( i = 0 ; i < sizeof(arr)/sizeof(*arr) ; i++ ) {
unsigned long sign, exponent, mantissa;
a.f = arr[i];
sign = ( a.d >> 31 ) & 0x01;
exponent = ( a.d >> 23 ) & 0xFF;
mantissa = a.d & 0x7FFFFF;
/* sign is only ever 0 or 1 */
/* exponent is 23 bits, but 24 are printed, so ignore the MSB */
printf("%1lx:%02lx:%06lx is %f\n", sign, exponent, mantissa, a.f );
}
return 0;
}
0:7c:000000 is 0.125000
0:7d:000000 is 0.250000
0:7e:000000 is 0.500000
0:7f:000000 is 1.000000
0:80:000000 is 2.000000
0:81:000000 is 4.000000
0:84:300000 is 44.000000
42 being 32 + 8 + 4
Taking the first nibble of the exponent (0011), we ignore the first bit (see the code comment), then we're left with 011
As a fraction, this is no half, + 1 quarter + 1 eighth (=0.375). Since we don't store the most significant bit, the true value of the mantissa is 1.375.
To get back to our decimal value, we now multiply this by some power of two (in this case, 2^5, or 32).
1.375 * 32 is indeed 44