问题
I have to represent a binary number in floating point number. I have a hexadecimal number as FFFF, when I am converting this hexadecimal number into Binary I am getting the corresponding binary number as 1111111111111111. The storage formats used by my Intel processor is 32 bits means 1 bit for the sign, 8 bits for the exponent, and 23 bits for the mantissa. I have some idea but quite confused. Can Anyone help me out what will be the corresponding float value for this binary number??
回答1:
32 ones
Simply try it out:
#include <stdio.h>
int main() {
union {
unsigned i;
float f;
} u;
u.i = 0xffffffff;
printf("%f\n", u.f);
return 0;
}
prints -nan
. This experiment assumes that you actually wanted 0xffffffff
not 0xffff
as your question states.
Looking at http://en.wikipedia.org/wiki/Single_precision you find that exponent 0xFF
together with a non-zero significand is treated as NaN.
16 ones
If you really are after 0xFFFF
only, as your question writes, then the code will print 0.000000
, but changing the %f
to %g
you get 9.18341e-41
. This is because both the integer and the float use the same endianess, i.e. you are talking about the float corresponding to the bit pattern 0x0000ffff
.
There you see that you'll now have zero sign (i.e. positive), zero exponent and a non-zero significand. According to the same wikipedia article, this represents a subnormal number. So this is really 0xffff ∙ 2−149 = 65535 ∙ 2−149.
回答2:
Assuming IEEE-754 32-bit float, 0xFFFFFFFF
(so 32 ones):
The first is the sign, so it's a negative float. Then comes the exponent, since it's the maximum value, it's a "special" exponent.
Because the significand is not zero, the result is NaN
.
Source: Wikipedia.
If you just want 16 ones, then:
Apparently, this is not the case as demonstrated by MvG.
Assuming the remaining bits are zero, the input is 0x0000FFFF
. The first bit is the sign, which is a zero. So we have a positive float.
Then we have 8 bits for the exponent. Since they are all zero, we'll have (depending on the fraction) either zero or a subnormal number.
Because the fraction is not all zeroes, this float is a subnormal number.
At this point, we've checked 9 bits, so there are still 7 extra zeroes.
The fraction is made of 7 zeroes, followed by 16 ones, therefore the significand is (in binary) 0.00000001111111111111111
. Multiply that by 2e-126
and you'll have your answer.
The result is, apparently, 9.18341e-41
.
来源:https://stackoverflow.com/questions/13673045/what-will-be-the-value-in-float-if-i-have-a-binary-number-as-1111111111111111-an