-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitmask.py
48 lines (34 loc) · 952 Bytes
/
bitmask.py
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
class BitMask:
def __init__(self, n: int):
self.__value = n
def __str__(self) -> str:
subset = []
for i in range(10):
if self.find(i):
subset.append(i)
return f'<BitMask>{{{self.__value=}, {subset=}}}'
@classmethod
def create(cls, seq: list[int]):
value = 0
for i in seq:
value = value ^ (1 << i)
return cls(value)
def find(self, v: int):
return self.__value & (1 << v)
def add(self, v: int):
if not self.find(v):
self.__value = self.__value ^ (1 << v)
def remove(self, v: int):
if self.find(v):
self.__value = self.__value ^ (1 << v)
if __name__ == '__main__':
for n in range(63 + 1):
print(n, bin(n))
print(BitMask.create([1, 3, 5, 7]))
bm = BitMask(30)
print(bm)
bm.add(5)
print(bm)
bm.remove(3)
bm.remove(3)
print(bm)