I want to convert numbers to binary. I searched the web and found a way to output unsignedshort numbers as binary, and I can convert those bitset variables to string.
I can also output the binary numbers for ints using similar code and cout but I cannot create bitset variables or strings in the same way as for unsigned short numbers, which is a problem as I want to be able to convert larger numbers.
Since writing this I managed to return the binary versions of long double variables
Don't you get a warning about losing information?
Because if you feed a long double to std::bitset then it will be converted to unsigned long. Try and see, for example, if the numbers 80.222 and 80.345 have the same "binary representation". I'm just making assumptions, because you have not posted that code yet, so I might be wrong.
Would still like to know how to do it for ints though if anyone can help.
Why not simply do: bitset<numeric_limits<int>::digits> test2(585);
About the unsigned short versus int:
- the first example works because 16 == numeric_limits<unsignedshort>::digits
- the second example doesn't work because 16 != numeric_limits<int>::digits
So you have a type mismatch, because it's not the same kind of bitset.