-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathBitmask_Basic.cpp
41 lines (36 loc) · 1.02 KB
/
Bitmask_Basic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Reference -> https://www.youtube.com/watch?v=7FmL-WpTTJ4
// We are using bitmask to store the numbers between 1 and 10 (in integers) [Each bit represents a subset of intergers 1 to 10]. 15 represents set with 1,2,3,4.]
// left shifting (x<<y) is equivalent to multiplying x with 2^y
// right shifting (x>>y) is equivalent to dividing x with 2^y.
#include<bits/stdc++.h>
using namespace std;
// If element is present it will get deleted and if its not there it will get added
void erase_or_add(int n, int& subset)
{
subset = subset ^ 1<<(n-1);
}
void display(int subset)
{
cout<<"The subset is: ";
// i is the ith bit from right to left
// 0th bit represents 1
for(int i=0;i<9;i++)
{
if(1<<i&subset)
cout << i+1<<" ";
}
cout<<"\n";
}
int main()
{
// num represents subset of numbers from 1 to 10
int num = 15;
display(num);
// delete the d from subset num
int d = 4;
erase_or_add(d,num);
display(num);
d=9;
erase_or_add(d,num);
display(num);
}