#include <bitset>
#include <cmath>
#include <iostream>
const int kExponentBits = 11;
const int kSignificandBits = 52;
std::bitset<kExponentBits + kSignificandBits + 1> FloatToBinaryIEEE754(double x) {
if (x == 0) {
return 0;
} else if (std::isinf(x)) {
return (1ull << kExponentBits + kSignificandBits) |
((x < 0) << (kExponentBits + kSignificandBits));
} else if (std::isnan(x)) {
return (1ull << kExponentBits + kSignificandBits) |
(1ull << (kExponentBits - 1)) | 1;
}
int exponent;
double significand = frexp(x, &exponent);
exponent += (1 << (kExponentBits - 1)) - 1;
std::bitset<kExponentBits + kSignificandBits + 1> result;
result = (unsigned long long)(significand * (1ull << kSignificandBits)) &
((1ull << kSignificandBits) - 1);
result |= (unsigned long long)(exponent) << kSignificandBits;
result |= (x < 0) << (kExponentBits + kSignificandBits);
return result;
}
int main() {
double x = 0.25;
std::bitset<kExponentBits + kSignificandBits + 1> binary = FloatToBinaryIEEE754(x);
std::cout << binary << std::endl;
return 0;
}