fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. bitset<8> bit1;
  6. bitset<8> bit3(15);
  7. bitset<8> bit4(string("10010011"));
  8. bitset<8> bit5("10010111");
  9.  
  10. cout << bit1 << " " << bit3 << " " << bit4 << " " << bit5 << endl;
  11.  
  12. bit1.set(7); // indexing is done from right to left. (7 6 5 4 3 2 1 0)
  13. cout << "set() : " << bit1 << endl;
  14.  
  15. bit3.reset(1);
  16. cout << "set() : " << bit3 << endl;
  17. bit3.set(1);//going back to initial.
  18.  
  19. bit4.flip(2);
  20. cout << "set() : " << bit4 << endl;
  21. bit4.flip(2);//going back to initial.
  22.  
  23. cout << bit5.count() << endl;
  24. return 0;
  25. }
Success #stdin #stdout 0s 5276KB
stdin
Standard input is empty
stdout
00000000 00001111 10010011 10010111
set() : 10000000
set() : 00001101
set() : 10010111
5