fork download
  1. #include <iostream>
  2. #include <bitset>
  3.  
  4. // Function to convert integer to IEEE 754 format
  5. std::bitset<32> int_to_ieee754(int num) {
  6. union {
  7. int i;
  8. float f;
  9. } converter;
  10. converter.i = num;
  11. return std::bitset<32>(converter.f);
  12. }
  13.  
  14. // Function to convert float to IEEE 754 format
  15. std::bitset<32> float_to_ieee754(float num) {
  16. union {
  17. float f;
  18. int i;
  19. } converter;
  20. converter.f = num;
  21. return std::bitset<32>(converter.i);
  22. }
  23.  
  24. int main() {
  25. // 计算表达式的结果
  26. float result = 137 + 315 + 299 + 340 / 4.0f;
  27.  
  28. // 将结果转换为IEEE 754格式
  29. std::bitset<32> result_ieee754 = float_to_ieee754(result);
  30.  
  31. // 输出结果
  32. std::cout << "结果的IEEE 754表示:" << result_ieee754 << std::endl;
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
结果的IEEE 754表示:01000100010100010000000000000000