Guest User

Untitled

a guest
Jan 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cstdlib>
  4. #include <iomanip>
  5. #include <iostream>
  6.  
  7. template <size_t size>
  8. void print_bin(int const a)
  9. {
  10. std::cout << '[' << std::bitset<size>(a) << ']';
  11. }
  12.  
  13. int main()
  14. {
  15. for (float i = -10.f; i <= 10.f; ++i)
  16. {
  17. int const a = *(reinterpret_cast<int const*>(&i));
  18. std::cout << std::setw(4) << i << ": ";
  19. int sign_bit = a >> 31;
  20. print_bin<1>(sign_bit);
  21. int exponent_bits = a >> 23;
  22. print_bin<8>(exponent_bits);
  23. print_bin<23>(a);
  24. std::cout << '\n';
  25. }
  26.  
  27. //int a = 127 << 23;
  28. //// Format: [0][00000000][00000000000000000000000]
  29. //// 1: [0][01111111][00000000000000000000000]
  30. //// 1: [0][01111111][00000000000000000000000]
  31. //float* b = reinterpret_cast<float*>(&a);
  32. //std::cout
  33. // << a << "\n"
  34. // << *b << "\n";
  35. }
  36.  
  37. // -10: [1][10000010][01000000000000000000000]
  38. // -9: [1][10000010][00100000000000000000000]
  39. // -8: [1][10000010][00000000000000000000000]
  40. // -7: [1][10000001][11000000000000000000000]
  41. // -6: [1][10000001][10000000000000000000000]
  42. // -5: [1][10000001][01000000000000000000000]
  43. // -4: [1][10000001][00000000000000000000000]
  44. // -3: [1][10000000][10000000000000000000000]
  45. // -2: [1][10000000][00000000000000000000000]
  46. // -1: [1][01111111][00000000000000000000000]
  47. // 0: [0][00000000][00000000000000000000000]
  48. // 1: [0][01111111][00000000000000000000000]
  49. // 2: [0][10000000][00000000000000000000000]
  50. // 3: [0][10000000][10000000000000000000000]
  51. // 4: [0][10000001][00000000000000000000000]
  52. // 5: [0][10000001][01000000000000000000000]
  53. // 6: [0][10000001][10000000000000000000000]
  54. // 7: [0][10000001][11000000000000000000000]
  55. // 8: [0][10000010][00000000000000000000000]
  56. // 9: [0][10000010][00100000000000000000000]
  57. // 10: [0][10000010][01000000000000000000000]
Add Comment
Please, Sign In to add comment