-
Notifications
You must be signed in to change notification settings - Fork 1
/
n-bit masking.cpp
48 lines (39 loc) · 1.02 KB
/
n-bit masking.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
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
struct NBitMasking {
int limit;
int base;
vector<int> power;
/**
initializes the masking system
with provided base and limit
limit is the highest power of the base that will be used
**/
NBitMasking(int base, int limit): base(base), limit(limit), power(limit + 1, 1) {
for (int i = 1; i <= limit; i++) {
power[i] = base * power[i - 1];
}
}
/**
0 - based
finds the pos-th bit of the number mask
**/
int getBit(int mask, int pos) {
return (mask / power[pos]) % base;
}
/**
0 - based
sets the pos-th bit of mask as bit and returns the new mask
**/
int setBit(int mask, int pos, int bit) {
int tmp = (mask / power[pos]) % base;
mask -= tmp * power[pos];
mask += bit * power[pos];
return mask;
}
};
int main() {
return 0;
}
// Test With:
// - [C. Anadi and Domino](https://codeforces.com/contest/1230/problem/C)