fork(1) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdint>
  4.  
  5. using namespace std;
  6.  
  7. void printFloatBits(float f) {
  8. // reinterpretujemy float jako 32-bitową liczbę całkowitą
  9. uint32_t bits = *reinterpret_cast<uint32_t*>(&f);
  10.  
  11. // znak
  12. cout << ((bits >> 31) & 1);
  13.  
  14. cout << " ";
  15.  
  16. // cecha (8 bitów)
  17. for (int i = 30; i >= 23; i--)
  18. cout << ((bits >> i) & 1);
  19.  
  20. cout << " ";
  21.  
  22. // mantysa (23 bity)
  23. for (int i = 22; i >= 0; i--)
  24. cout << ((bits >> i) & 1);
  25.  
  26. cout << endl;
  27. }
  28.  
  29. int main() {
  30. float x;
  31.  
  32. cout << "Podaj liczbe dziesietna: ";
  33. cin >> x;
  34.  
  35. cout << fixed << setprecision(15);
  36. cout << "Wartosc jako float = " << x << endl;
  37.  
  38. cout << "Reprezentacja IEEE 754 (float, 32 bity):" << endl;
  39. printFloatBits(x);
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5288KB
stdin
5
stdout
Podaj liczbe dziesietna: Wartosc jako float = 5.000000000000000
Reprezentacja IEEE 754 (float, 32 bity):
0 10000001 01000000000000000000000