Monday, December 30, 2013

Floating Point Binary Representation in C++

Interested in how a float point number is stored in memory? The following C++ code can print it out.

#include <iostream>
#include <bitset>

using namespace std;

int main() {
    float f = 3.14159;
    bitset<32> bs(*(int*)&f);
    cout << bs << endl;
}

The output is 01000000010010010000111111010000.

No comments:

Post a Comment