This question already has an answer here:
- Floating Point to Binary Value(C++) 11 answers
I am working on a project of converting any real number into binary using IEEE 754 format.
My first trial is using the bitset
library type for conversion of the number then i can worry about dividing the whole number into sign bit, exponent and mantissa.
int foo;
cin >> foo;
bitset<32> my_bit(foo);
As it turns out, bitset
will do with signed
integers only.
How do i include floating point numbers?
Can i accomplish my task with another library type that is as fairly simple as bitset
?
Actually,
bitset
constructor acceptsunsigned long
in C++ 03 andunsigned long long
in C++ 11. Now, as for storingfloat
in abitset
, this should do the trick: