Advertisement
maiden18

bitset

Nov 13th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     // bitmask is indexed from 0 from least significant bit
  7.     // all and to_ullong is c++11 feature, ulong can be used instead of ullong
  8.  
  9.     // declaring
  10.     bitset<6>bb;
  11.  
  12.     // input
  13.     cin>>bb;
  14.  
  15.     // check if all or any or none of bb is 1
  16.     cout<<bb.all()<<" "<<bb.any()<<" "<<bb.none()<<"\n";
  17.  
  18.     //count number of 1's
  19.     cout<<bb.count()<<" "<<bb.size()-bb.count()<<"\n";
  20.  
  21.     // flip all or single bit
  22.     bb.flip();
  23.     cout<<bb<<"\n";
  24.     cout<<bb.flip(2)<<"\n";
  25.  
  26.     // operator[]
  27.     bb[1]=bb[0]=0;
  28.     cout<<bb[0]<<" "<<bb<<"\n";
  29.  
  30.     // reset or set all bits
  31.     bb.reset();
  32.     cout<<bb<<"\n";
  33.     bb.set();
  34.     cout<<bb<<"\n";
  35.  
  36.     // to unsigned long long int
  37.     cout<<bb.to_ullong()<<"\n";
  38.  
  39.     // bitwise operators
  40.     bb&=bb<<2;
  41.     cout<<bb<<"\n";
  42.  
  43.     // find first set bit, returns bb.size() if there is none
  44.     cout<<bb._Find_first()<<"\n";
  45.  
  46.     //get next set bit after a certain index, returns bb.size() if there is none
  47.     cout<<bb._Find_next(3)<<"\n";
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement