Well, for single precision floats you've got:
Code:
#include <bitset>
#include <iostream>
#include <limits>
using namespace std;
int main(){
float f1 = 1.0f;
float f2 = -1.0f;
cout << "1: \n"
<< bitset<32>(*(reinterpret_cast<unsigned long*>(&f1)))
<< endl;
cout << "-1: \n"
<< bitset<32>(*(reinterpret_cast<unsigned long*>(&f2)))
<< endl;
return 0;
}
My output:
Code:
1:
00111111100000000000000000000000
-1:
10111111100000000000000000000000
On my system unsigned long is 4 bytes (32 bits) which is what you initialize a bitset with and fits nicely with the size of a single precision float. You can't do the above quite so easily with a double since it's 64 bits (you can only initialize the bitset with up to 32 of those 64 bits) so you need a bit more trickery to break up the 64 bit double value into something that can be used to initialize the bitset with:
Code:
double d1 = 1.0;
double d2 = -1.0;
cout << "1:\n"
<< bitset<32>(*(reinterpret_cast<unsigned long*>(&d1)+1))
<< bitset<32>(*reinterpret_cast<unsigned long*>(&d1)) << endl;
cout << "-1:\n"
<< bitset<32>(*(reinterpret_cast<unsigned long*>(&d2)+1))
<< bitset<32>(*reinterpret_cast<unsigned long*>(&d2)) << endl;
My output:
Code:
1:
0011111111110000000000000000000000000000000000000000000000000000
-1:
1011111111110000000000000000000000000000000000000000000000000000
In both the above instances (single and double precision floats/doubles), the values for 1 and -1 are off by a single bit like you would expect.