Another binary question

I want to convert numbers to binary. I searched the web and found a way to output unsigned short 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.

Code below, commented out doesn't compile...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bitset>
#include <limits>
#include <iostream>
#include <string>
using namespace std;

int main() 
{
	bitset<16> test;
	bitset<16> test2;
	string test1str, test2str;
	
	cout << bitset<numeric_limits<int>::digits>(585) << endl;
	
	test = bitset<numeric_limits<unsigned short>::digits>(585);
	
	cout << test << endl;
	
	test1str = test.to_string();
	
	cout << test1str << endl;
	
	//test2 = bitset<numeric_limits<int>::digits>(585);
	
	//cout << test2 << endl;
	
	//test2str = test2.to_string();
		
	//cout << test2str;

	return 0; 	
}
Since writing this I managed to return the binary versions of long double variables by using bitset<64> instead of bitset<16>, and

bitset<numeric_limits<long double>::digits>(585)

instead of

bitset<numeric_limits<unsigned short>::digits>(585)

Would still like to know how to do it for ints though if anyone can help.
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);
Thanks fro your reply.

For my purposes I was only interested in whole numbers, but you are right...

1
2
3
4
5
6
7
	test2 = bitset<numeric_limits<long double>::digits>(585.222);
	
	cout << test2 << endl;
	
	test2 = bitset<numeric_limits<long double>::digits>(585.345);
		
	cout << test2 << endl;


...prints the same binary number. But I didn't get any warnings about losing info.

Your suggestion for ints...

bitset<numeric_limits<int>::digits> test2(585);

...doesn't seem to work
I don't understand why this works:

cout << bitset<numeric_limits<int>::digits>(585) << endl;

And this does:

1
2
3
4
bitset<16> test;
test = bitset<numeric_limits<unsigned short>::digits>(585);
	
	cout << test << endl;


But this doesn't:

1
2
3
4
bitset<16> test;
test = bitset<numeric_limits<int>::digits>(585);
	
	cout << test << endl;

I don't understand why this works:

About the unsigned short versus int:
- the first example works because 16 == numeric_limits<unsigned short>::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.

bitset<numeric_limits<int>::digits> test2(585);

...doesn't seem to work

It does seem to work for me?
http://ideone.com/R3fEYb

Edit: small omission corrected.
Last edited on
OK thanks, that's better, it wasn't working because I was still declaring test2 as a bitset variable and then trying to use your line of code.

In case it is useful to anyone else, here is the now working code to display binary numbers using any number variable type:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bitset>
#include <limits>
#include <iostream>
using namespace std;

int main() 
{
	string test1str;
	
	cout << bitset<numeric_limits<int>::digits>(585) << endl;
	
	bitset<numeric_limits<int>::digits>test(585);
	
	cout << test << endl;
	
	test1str = test.to_string();
	
	cout << test1str << endl;
	
	bitset<numeric_limits<long double>::digits>test2(585);
	
	cout << test2 << endl;

	return 0; 	
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <bitset>
#include <limits>

int main()
{
    constexpr int i = std::numeric_limits<int>::min() ;

    const std::bitset< std::numeric_limits<int>::digits > bad(i) ;
    std::cout << bad << '\n' ;

    const std::bitset< std::numeric_limits<unsigned int>::digits > good(i) ;
    std::cout << good << '\n' ;
}

http://coliru.stacked-crooked.com/a/743e6a4d64e2aa47
Topic archived. No new replies allowed.