[try Beta version]
Not logged in

 
ccc

Jun 18, 2016 at 5:17am
ccc
Last edited on Jun 18, 2016 at 10:45pm
Jun 18, 2016 at 5:24am
The template argument needs to be a compile time constant, whereas in this case, b is a variable.
Why not just set the size to be sizeof(int)*CHAR_BIT ?
Last edited on Jun 18, 2016 at 5:26am
Jun 18, 2016 at 5:28am
Try this:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <bitset>
#include <climits>
using namespace std;

int main()
{
    int x = 12;
    cout << bitset<sizeof(int)*CHAR_BIT> (x);
}



EDIT: If you want to remove the preceding zeroes, you could use a stringstream to convert a bitset to a string, then use find the first 1 and truncate the string with substr
Last edited on Jun 18, 2016 at 5:32am
Jun 18, 2016 at 5:29am
b read for keyboard but no run. What is the code
Jun 18, 2016 at 5:33am
Try the code above, it works without any input
Jun 18, 2016 at 5:39am
Read B whit CIN
Jun 18, 2016 at 5:40am
Dead integer for define number bits whit use CIN
Jun 18, 2016 at 5:42am

I require read input , the number of bits to use ...
Jun 19, 2016 at 8:04am
@zionet

Please re-edit your post and let other see it in future....

there will be other people in future who will someday find similar problem and could get help from your post
Jun 20, 2016 at 2:45am
Original Topic Title: "Convert INT to BINARY with BITSET ERROR"

OP:

What is problem no read <b> b is a bits number

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <bitset>
using namespace std;

int main()
{
int b,x = 12;
cin>>b;
cout<< bitset<b> (x);
}
Jun 20, 2016 at 3:40am
wow! did you remember all that stuff???
Jun 20, 2016 at 3:42am
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/193018/
Jun 20, 2016 at 4:02am
ooh!
Topic archived. No new replies allowed.