Skip to content

Commit 81b151b

Browse files
committed
Important Bitmasking algorithms added!
1 parent 0a7d657 commit 81b151b

14 files changed

+454
-0
lines changed

Bitmasking/1. Unique number I.txt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
/*
5+
Every number occurs twice in the array except one number
6+
find out that number
7+
*/
8+
9+
int uniqueNumber(int ar[], int n)
10+
{
11+
int ans = 0;
12+
for(int i=0; i<n; i++)
13+
{
14+
ans = ans ^ ar[i];
15+
}
16+
return ans;
17+
}
18+
19+
int main()
20+
{
21+
int arr[]= {1,5,3,5,8,9,8,1,3};
22+
int n = sizeof(arr)/sizeof(int);
23+
24+
cout<<uniqueNumber(arr, n);
25+
return 0;
26+
}

Bitmasking/10. Unique Number III.txt

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
/*
5+
Every number is occuring thrice in the given array except one. find the unique number
6+
*/
7+
8+
int main()
9+
{
10+
int count[64] = {0};
11+
12+
int n;
13+
cin>>n;
14+
int no;
15+
for(int i=0; i<n; i++)
16+
{
17+
cin>>no;
18+
//we will keep adding the bits of upcoming numbers in the count array
19+
20+
int j=0;
21+
while(no>0)
22+
{
23+
count[j]+= (no&1);
24+
j++;
25+
no = no>>1;
26+
}
27+
}
28+
29+
//now lets take mod with 3 of numbers present in the count array
30+
int ans = 0;
31+
int p = 1;
32+
for(int i=0; i<n; i++)
33+
{
34+
count[i] = count[i]%3;
35+
ans+=count[i]*p;
36+
p*=2;
37+
}
38+
cout<<ans;
39+
40+
return 0;
41+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int fastExponentiation(int a, int n)
5+
{
6+
int ans = 1;
7+
8+
while(n>0)
9+
{
10+
if((n&1)==1)
11+
{
12+
ans*=a;
13+
}
14+
n>>=1;
15+
a*=a;
16+
}
17+
return ans;
18+
}
19+
20+
int main()
21+
{
22+
23+
int a,n;
24+
cin>>a>>n;
25+
cout<<fastExponentiation(a,n);
26+
return 0;
27+
}

Bitmasking/12. generate subsets.txt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void filterChars(char c[], int n)
5+
{
6+
int j=0;
7+
while(n>0)
8+
{
9+
if(n&1)
10+
{
11+
cout<<c[j];
12+
}
13+
j++;
14+
n>>=1;
15+
}
16+
cout<<endl;
17+
}
18+
19+
void printSubsequences(char c[])
20+
{
21+
int n = strlen(c);
22+
for(int i=0; i<(1<<n); i++)
23+
{
24+
filterChars(c,i);
25+
}
26+
}
27+
28+
int main()
29+
{
30+
char c[100];
31+
cin>>c;
32+
printSubsequences(c);
33+
return 0;
34+
}

Bitmasking/13. codechef problem.txt

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
5+
#define endl "\n"
6+
#define int long long
7+
8+
/*
9+
problem link:
10+
https://www.codechef.com/COOK128B/problems/OROFAND
11+
*/
12+
13+
int32_t main()
14+
{
15+
IOS;
16+
int t;
17+
cin>>t;
18+
while(t--)
19+
{
20+
int n,q;
21+
cin>>n>>q;
22+
23+
int arr[n];
24+
for(int i=0; i<n; i++)
25+
{
26+
cin>>arr[i];
27+
}
28+
29+
int bit[32] = {0};
30+
for(int i=0; i<n; i++)
31+
{
32+
for(int j=0; j<32; j++)
33+
{
34+
if((1<<j)&arr[i]) bit[j]++;
35+
}
36+
}
37+
38+
int ans = 0;
39+
for(int i=0; i<32; i++)
40+
{
41+
if(bit[i]>0)
42+
{
43+
ans+=(1<<i);
44+
}
45+
}
46+
47+
cout<<ans<<endl;
48+
49+
while(q--)
50+
{
51+
int x,v;
52+
cin>>x>>v;
53+
x--;
54+
55+
for(int i=0; i<32; i++)
56+
{
57+
if(((1<<i)&arr[x]))
58+
bit[i]--;
59+
}
60+
61+
arr[x]=v;
62+
63+
for(int i=0; i<32; i++)
64+
{
65+
if(((1<<i)&v)) bit[i]++;
66+
}
67+
68+
ans = 0;
69+
for(int i=0; i<32; i++)
70+
{
71+
if(bit[i]>0)
72+
{
73+
ans+=(1<<i);
74+
}
75+
}
76+
cout<<ans<<endl;
77+
}
78+
}
79+
return 0;
80+
}
81+

Bitmasking/2. get ith bit.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
//task is to find ith bit (i starting from zero and from right side) in a number
5+
6+
int ithBit(int n, int i)
7+
{
8+
int mask = 1<<i;
9+
int bit = (n & mask) > 0 ? 1 : 0;
10+
11+
return bit;
12+
}
13+
14+
int main()
15+
{
16+
int n,i;
17+
cin>>n>>i;
18+
cout<<ithBit(n,i);
19+
return 0;
20+
}

Bitmasking/2. set ith bit.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int setIthBit(int n, int i)
5+
{
6+
int mask = 1<<i;
7+
n = n | mask;
8+
9+
return n;
10+
}
11+
12+
int main()
13+
{
14+
int n,i;
15+
cin>>n>>i;
16+
cout<<setIthBit(n,i);
17+
return 0;
18+
}

Bitmasking/3. clear ith bit.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int clearIthBit(int n, int i)
5+
{
6+
int mask = ~(1<<i);
7+
n = n & mask;
8+
9+
return n;
10+
}
11+
12+
int main()
13+
{
14+
int n,i;
15+
cin>>n>>i;
16+
cout<<clearIthBit(n,i);
17+
return 0;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int updateIthBitByValue(int n, int i, int v)
5+
{
6+
//lets first cleat ith bit
7+
int mask = (~(1<<i));
8+
n = (n&mask);
9+
10+
int newmask = (v<<i);
11+
n = (n|newmask);
12+
13+
return n;
14+
}
15+
16+
int main()
17+
{
18+
int n,i,v;
19+
cin>>n>>i>>v;
20+
21+
cout<<updateIthBitByValue(n,i,v);
22+
return 0;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int clearLastIBits(int n, int i)
5+
{
6+
//lets take mask with all ones
7+
int mask = -1;
8+
9+
//lets clear last i bits of our mask
10+
mask = (mask<<i);
11+
12+
//take AND with of your number with mask
13+
n = n&mask;
14+
15+
return n;
16+
}
17+
18+
int main()
19+
{
20+
int n,i;
21+
cin>>n>>i;
22+
cout<<clearLastIBits(n,i);
23+
return 0;
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int clearBitsFromItoJ(int n, int i, int j)
5+
{
6+
//lets take one mask (a) as all ones with j+1 number of trailing zeros
7+
int a = (~0)<<(j+1);
8+
9+
//lets take mask (b) as all zero with i number of trailing ones
10+
int b = (1<<i)-1;
11+
12+
//required mask will be a|b
13+
int mask = a|b;
14+
15+
n = n&mask;
16+
17+
return n;
18+
}
19+
20+
int main()
21+
{
22+
int n,i,j;
23+
cin>>n>>i>>j;
24+
cout<<clearBitsFromItoJ(n,i,j);
25+
return 0;
26+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int replaceNbitsByMFromItoJ(int n, int m, int i, int j)
5+
{
6+
//lets first clear the bits in n from i to j
7+
int a = (~0)<<(j+1);
8+
9+
int b = (1<<i) -1;
10+
int mask = a|b;
11+
12+
n = n&mask;
13+
14+
m = (m<<i);
15+
16+
return n|m;
17+
}
18+
19+
int main()
20+
{
21+
int n = 15;
22+
int m = 2;
23+
int i = 1;
24+
int j = 3;
25+
26+
cout<<replaceNbitsByMFromItoJ(n,m,i,j);
27+
return 0;
28+
}

0 commit comments

Comments
 (0)