[Source]
// // Single precision floating-point format // // http://en.wikipedia.org/wiki/Single_precision_floating-point_format // // ---------+------------------+-------------------------+---------------------------------------------------- // exponent | fraction zero | fraction non-zero | equation // ---------+------------------+-------------------------+---------------------------------------------------- // 00H | zero, ?0 | subnormal numbers | (-1)signbits×2^(?126) * 0.fractionbits // ---------+------------------+-------------------------+---------------------------------------------------- // 01H~FEH | normalized value | (-1)signbits×2^(exponentbits?127) * 1.fractionbits // ---------+------------------+-------------------------+---------------------------------------------------- // FFH | +-infinity | NaN (quiet, signalling) | // ---------+------------------+-------------------------+---------------------------------------------------- // #include <iostream> // for std::cin, std::cout #include <sstream> // for std::stringstream #include <string> // for std::string typedef unsigned int uint32; std::string fraction_bits(int fraction) { if (fraction == 0) return ""; std::string res = "."; int mask = 0x00400000; while (fraction != 0) { if (fraction & mask) res += "1"; else res += "0"; fraction &= ~mask; mask >>= 1; } return res; } std::string int_to_str(int i) { std::string s; std::stringstream out; out << i; s = out.str(); return s; } void writeSingle(float value) { uint32 u; int s; // sign int e; // exponent int f; // fraction(significand) // // extract bits // u = *(uint32*)&value; // // extract sign // s = (u & 0x80000000) >> 31; // 1 bit // // extract exponent // e = (u & 0x7F800000) >> 23; // 8 bit // // extract fraction(significand) // f = (u & 0x007FFFFF); // 23 bit // // write single value // std::cout << value << " = "; if (e == 255) { if (f != 0) { std::cout << "NaN" << std::endl; // Not-a-Number return; } else if (s == 0) { std::cout << "+INF" << std::endl; // positive infinity return; } else // s == 1 { std::cout << "-INF" << std::endl; // negative infinity return; } } std::string str_sign = (s == 0) ? " " : "-"; std::string str_exponent = (e == 0) ? "-126" : int_to_str(e - 127); std::string str_implicit_leading_bit = (e == 0) ? "0" : "1"; std::string str_fraction_bits = fraction_bits(f); std::cout << str_sign << "2^(" << str_exponent << ") * " << str_implicit_leading_bit << str_fraction_bits << "(b)" << std::endl; } int main() { float value; while (true) { std::cin >> value; writeSingle(value); if (value == 0) break; } return 0; }
[Result]
1 = 2^(0) * 1(b)
2 = 2^(1) * 1(b)
3 = 2^(1) * 1.1(b)
4 = 2^(2) * 1(b)
5 = 2^(2) * 1.01(b)
6 = 2^(2) * 1.1(b)
7 = 2^(2) * 1.11(b)
8 = 2^(3) * 1(b)
9 = 2^(3) * 1.001(b)
10 = 2^(3) * 1.01(b)
-1 = -2^(0) * 1(b)
-2 = -2^(1) * 1(b)
-3 = -2^(1) * 1.1(b)
-4 = -2^(2) * 1(b)
-5 = -2^(2) * 1.01(b)
-6 = -2^(2) * 1.1(b)
-7 = -2^(2) * 1.11(b)
-8 = -2^(3) * 1(b)
-9 = -2^(3) * 1.001(b)
-10 = -2^(3) * 1.01(b)
1 = 2^(0) * 1(b)
2 = 2^(1) * 1(b)
4 = 2^(2) * 1(b)
8 = 2^(3) * 1(b)
16 = 2^(4) * 1(b)
32 = 2^(5) * 1(b)
64 = 2^(6) * 1(b)
128 = 2^(7) * 1(b)
256 = 2^(8) * 1(b)
0.5 = 2^(-1) * 1(b)
0.25 = 2^(-2) * 1(b)
0.125 = 2^(-3) * 1(b)
0.0625 = 2^(-4) * 1(b)
0.03125 = 2^(-5) * 1(b)
0.015625 = 2^(-6) * 1(b)
0.0078125 = 2^(-7) * 1(b)
0.00390625 = 2^(-8) * 1(b)
0.1 = 2^(-4) * 1.10011001100110011001101(b)
0.2 = 2^(-3) * 1.10011001100110011001101(b)
0.3 = 2^(-2) * 1.0011001100110011001101(b)
0.4 = 2^(-2) * 1.10011001100110011001101(b)
0.5 = 2^(-1) * 1(b)
0.6 = 2^(-1) * 1.0011001100110011001101(b)
0.7 = 2^(-1) * 1.01100110011001100110011(b)
0.8 = 2^(-1) * 1.10011001100110011001101(b)
0.9 = 2^(-1) * 1.1100110011001100110011(b)
69.8 = 2^(6) * 1.0001011100110011001101(b)
0 = 2^(-126) * 0(b)
[Download]