Advertisement
Imran_Mohammed

Bitset

Oct 5th, 2022
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5.  
  6.     //Declaration :
  7.     bitset < 32 > b;//all are 0 in 32 bit
  8.  
  9.     bitset < 32 > b(5);
  10.     cout << b << endl;//binary of 5 in 32 bit
  11.  
  12.     bitset < 32 > b( string("101011") );
  13.     cout << b.to_ulong() << endl;//Binary to decimal by using String bitset
  14.  
  15.     bitset < 32 > bs;
  16.     cout << bs.to_ulong() << endl;//0
  17.  
  18.     bs.set();
  19.     cout << bs << endl;// all are 1
  20.  
  21.     bs.reset();
  22.     cout << bs << endl;
  23.  
  24.     //How many 1 in this number when it is binary
  25.     bitset < 6 > b(10);
  26.     cout << b.count() << endl;//2
  27.  
  28.     // 1 and 0 just inter change
  29.     b.flip();
  30.     cout << b << endl;
  31.     //LSB
  32.     cout << b[0] << " " << b[1] << endl;
  33.  
  34.  
  35.     return 0;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement