fork(1) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <bitset>
  4. using namespace std;
  5.  
  6. void pokazIEEE754(float x)
  7. {
  8. // reinterpretujemy float jako 32-bitową liczbę całkowitą
  9. uint32_t bits = *reinterpret_cast<uint32_t*>(&x);
  10.  
  11. cout << "Liczba: " << x << endl;
  12.  
  13. bitset<32> b(bits);
  14.  
  15. cout << "Bity IEEE-754: " << b << endl;
  16.  
  17. cout << "Znak: " << b[31] << endl;
  18. cout << "Wykładnik: " << b.to_string().substr(1, 8) << endl;
  19. cout << "Mantysa: " << b.to_string().substr(9, 23) << endl;
  20. cout << endl;
  21. }
  22.  
  23. int main()
  24. {
  25. float a = 10.25f;
  26. float b = -0.2f;
  27.  
  28. pokazIEEE754(a);
  29. pokazIEEE754(b);
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0.01s 5124KB
stdin
Standard input is empty
stdout
Liczba: 10.25
Bity IEEE-754: 01000001001001000000000000000000
Znak:      0
Wykładnik: 10000010
Mantysa:   01001000000000000000000

Liczba: -0.2
Bity IEEE-754: 10111110010011001100110011001101
Znak:      1
Wykładnik: 01111100
Mantysa:   10011001100110011001101