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 190 views
1 answer 175 views
1 answer 223 views
1 answer 162 views
162 views asked Dec 6, 2019 by avibootz
1 answer 192 views
1 answer 177 views
177 views asked Apr 18, 2018 by avibootz
...