Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <bitset>
- #include <cstdlib>
- #include <iomanip>
- #include <iostream>
- template <size_t size>
- void print_bin(int const a)
- {
- std::cout << '[' << std::bitset<size>(a) << ']';
- }
- int main()
- {
- for (float i = -10.f; i <= 10.f; ++i)
- {
- int const a = *(reinterpret_cast<int const*>(&i));
- std::cout << std::setw(4) << i << ": ";
- int sign_bit = a >> 31;
- print_bin<1>(sign_bit);
- int exponent_bits = a >> 23;
- print_bin<8>(exponent_bits);
- print_bin<23>(a);
- std::cout << '\n';
- }
- //int a = 127 << 23;
- //// Format: [0][00000000][00000000000000000000000]
- //// 1: [0][01111111][00000000000000000000000]
- //// 1: [0][01111111][00000000000000000000000]
- //float* b = reinterpret_cast<float*>(&a);
- //std::cout
- // << a << "\n"
- // << *b << "\n";
- }
- // -10: [1][10000010][01000000000000000000000]
- // -9: [1][10000010][00100000000000000000000]
- // -8: [1][10000010][00000000000000000000000]
- // -7: [1][10000001][11000000000000000000000]
- // -6: [1][10000001][10000000000000000000000]
- // -5: [1][10000001][01000000000000000000000]
- // -4: [1][10000001][00000000000000000000000]
- // -3: [1][10000000][10000000000000000000000]
- // -2: [1][10000000][00000000000000000000000]
- // -1: [1][01111111][00000000000000000000000]
- // 0: [0][00000000][00000000000000000000000]
- // 1: [0][01111111][00000000000000000000000]
- // 2: [0][10000000][00000000000000000000000]
- // 3: [0][10000000][10000000000000000000000]
- // 4: [0][10000001][00000000000000000000000]
- // 5: [0][10000001][01000000000000000000000]
- // 6: [0][10000001][10000000000000000000000]
- // 7: [0][10000001][11000000000000000000000]
- // 8: [0][10000010][00000000000000000000000]
- // 9: [0][10000010][00100000000000000000000]
- // 10: [0][10000010][01000000000000000000000]
Add Comment
Please, Sign In to add comment