Skip to content

Commit

Permalink
Add bitset
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravUppoor committed Aug 12, 2021
1 parent 6b0b17e commit 789e3e3
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions Data Structures/stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include<vector>
#include<set>
#include<map>
#include<tuple>
#include<bitset>
const long long INT_MAX = 1e5 + 10;

using namespace std;
Expand Down Expand Up @@ -106,6 +108,60 @@ void mapDemo() {
cout << cnt['a'] <<"\n";
}

void tupleDemo() {
typedef tuple<int, string, string, string> state;
vector<state> names;
names.push_back(make_tuple(1, "saurav", "rooppus", "uppoor"));
names.push_back(make_tuple(2, "pratik", "kaiyu", "kundnani"));
names.push_back(make_tuple(3, "saket", "cdstromer", "thota"));
names.push_back(make_tuple(4, "manjunath", "dumped", "vasam"));

for(auto x: names) cout << get<0>(x) << ' ' << get<1>(x) << ' ' << get<2>(x) << '\n';
}

void bitsetDemo() {
bitset<32> bs(-3);
cout << bs << '\n'; // 00000

cout << bs.count() << '\n'; // 0
int x = static_cast<int>(bs.to_ulong()) << '\n';
/* INBUILT FUNCTIONS
any() --> true if any bit is set
none() --> true if no bit is set
set(i) --> set ith bit if provided else set all
reset(i) --> reset ith bit
flip(i) --> toggle ith bit
.to_ulong() --> convert to unsigned long (then convert to signed if needed)
*/
/* BITWISE OPERATIONS */
bitset<4> bset1(9); // bset1 contains 1001
bitset<4> bset2(3); // bset2 contains 0011

// comparison operator
cout << (bset1 == bset2) << endl; // false 0
cout << (bset1 != bset2) << endl; // true 1

// bitwise operation and assignment
cout << (bset1 ^= bset2) << endl; // 1010
cout << (bset1 &= bset2) << endl; // 0010
cout << (bset1 |= bset2) << endl; // 0011

// left and right shifting
cout << (bset1 <<= 2) << endl; // 1100
cout << (bset1 >>= 1) << endl; // 0110

// not operator
cout << (~bset2) << endl; // 1100

// bitwise operator
cout << (bset1 & bset2) << endl; // 0010
cout << (bset1 | bset2) << endl; // 0111
cout << (bset1 ^ bset2) << endl; // 0101

}

void PowerOfStl() {
/*
add[10, 100]
Expand All @@ -123,7 +179,7 @@ void PowerOfStl() {
auto itr = S.upper_bound({point, INT_MAX});
if(itr == S.begin()) {
cout <<"Not in range\n";
return 0;
return;
}

itr--;
Expand All @@ -136,7 +192,7 @@ void PowerOfStl() {
}
}


int main() {

return 0;
bitsetDemo();
}

0 comments on commit 789e3e3

Please sign in to comment.