fork download
  1. #include <iostream>
  2. #include <bitset>
  3. #include <cstdint>
  4. using namespace std;
  5.  
  6. union FloatUnion {
  7. float f;
  8. uint32_t u;
  9. };
  10.  
  11. void printFloatBits(float value) {
  12. FloatUnion fu;
  13. fu.f = value;
  14.  
  15. bitset<32> bits(fu.u);
  16.  
  17. // Bity IEEE 754: [sign | exponent | mantissa]
  18. string s = bits.to_string();
  19. cout << "Wartosc: " << value << endl;
  20. cout << "Bity IEEE 754 (float 32):\n";
  21. cout << "Znak: " << s[0] << endl;
  22. cout << "Wykladnik:" << s.substr(1, 8) << endl;
  23. cout << "Mantysa: " << s.substr(9) << endl;
  24. cout << "Cala reprezentacja: " << s << endl << endl;
  25. }
  26.  
  27. int main() {
  28. printFloatBits(10.25f);
  29. printFloatBits(-0.2f);
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Wartosc: 10.25
Bity IEEE 754 (float 32):
Znak:     0
Wykladnik:10000010
Mantysa:  01001000000000000000000
Cala reprezentacja: 01000001001001000000000000000000

Wartosc: -0.2
Bity IEEE 754 (float 32):
Znak:     1
Wykladnik:01111100
Mantysa:  10011001100110011001101
Cala reprezentacja: 10111110010011001100110011001101