Overview: Today we practice floating point representation today. Develop and run your code on your preferred C++ environ
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Overview: Today we practice floating point representation today. Develop and run your code on your preferred C++ environ
Practice 1: Develop a C++ program to convert an integer to 32-bit IEEE-754 floating point representation. Print sign, exponent, and fraction and the end of your program. Verify your code by converting integer number 98765 to IEEE-754 and confirm your conversion is correct by verifying on the IEEE754 online calculator: https://www.h-schmidt.net/Float Converter/IEEE754.html What to submit on Moodle? Submit your C++ code on Moodle. Hints One algorithm is to convert decimal to string stream and then normalize and find exponent and fraction. Useful tips: To print a number in binary for debugging, use bitset. Example: 1. #include 2. #include 3. 4. int main() { 5. 9. int a 54; std::cout << std::bitset<32>(a) << std::endl; return 0;
1. #include 2. #include 3. 4. int main() { 5 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40 41. 42. 43. 44. 45. 46. 47. 48. 49. 50 binary operations that are useful here: original int a 54; std::cout << std::bitset<64>(a) << "< original" << std::endl; //shift left and insert 1 in least significant bit int b (a << 1) | 0x00000001; std::cout << std::bitset<64>(b) << "< shift-left insert 1" << std::endl; // shift left and insert 0 in Teast significant bit int g (a << 1) & 0xfffffffe; std::cout << std::bitset<64>(g) << "< shift-left insert 0" << std::endl; // load into upper 64-bit long long intca; CC << 32: std::cout<(c) <<< insert into upper 32-bit" << std::endl; // test least-significant biz (LSB) int d = 5; std::cout << std::bitset<32>(d) << " < original value" << std::endl; if ((d & 0x00000001)-1) std::cout << "1sb of number above is one\n"; } else ( } std::cout << "1sb of number above is zero\n": //shift right int e = d; std::cout << std::bitset<32>(e) <<< original number" << std::endl; ee >> 1; // shift to the right } else { } std::cout << std::bitset<32>(e) <<< shift right" << std::endl; if ((e & 0x00000001) 1) { std::cout << "1sb of number above is one\n": std::cout << "1sb of number above is zero\n": return 0;