How to initialize bitset with bits in C++

5 Answers

0 votes
#include <bits/stdc++.h> 
 
using namespace std; 
   
int main() 
{ 
    bitset<4> bs1(string("1100")); 
    bitset<6> bs2(string("101101")); 

    cout << bs1 << endl;
    cout << bs2 << endl;

    return 0; 
} 
 
 
 
/*
run:
 
1100
101101
 
*/

 



answered Dec 6, 2019 by avibootz
0 votes
#include <bits/stdc++.h> 
  
using namespace std; 
    
int main() 
{ 
    string s = "100110"; 
    bitset<6> bs(s); 
 
    cout << bs << endl;
 
    return 0; 
} 
  
  
  
/*
run:
  
100110
  
*/

 



answered Dec 7, 2019 by avibootz
0 votes
#include <bits/stdc++.h> 
  
using namespace std; 
    
int main() 
{ 
    string s = "1101"; 
    bitset<6> bs(s, 1);   
 
    cout << bs << endl;
 
    return 0; 
} 
  
  
  
/*
run:
  
000101
  
*/

 



answered Dec 7, 2019 by avibootz
0 votes
#include <bits/stdc++.h> 
  
using namespace std; 
    
int main() 
{ 
    string s = "11101"; 
    bitset<6> bs(s, 2, 3);   
 
    cout << bs << endl;
 
    return 0; 
} 
  
  
  
/*
run:
  
000101
  
*/

 



answered Dec 7, 2019 by avibootz
0 votes
#include <bits/stdc++.h> 
  
using namespace std; 
    
int main() 
{ 
    bitset<6> bs(3);   
 
    cout << bs << endl;
 
    return 0; 
} 
  
  
  
/*
run:
  
000011
  
*/

 



answered Dec 7, 2019 by avibootz

Related questions

1 answer 173 views
1 answer 158 views
1 answer 201 views
1 answer 153 views
153 views asked Dec 6, 2019 by avibootz
1 answer 167 views
1 answer 160 views
160 views asked Apr 18, 2018 by avibootz
...