-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmask.py
More file actions
26 lines (25 loc) · 770 Bytes
/
bitmask.py
File metadata and controls
26 lines (25 loc) · 770 Bytes
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
class BitMask(int):
def NOT(self):
return BitMask(-self)
def AND(self, bm):
return BitMask(self & bm)
def OR(self, bm):
return BitMask(self | bm)
def XOR(self, bm):
return BitMask(self ^ bm)
def shiftleft(self, num):
return BitMask(self << num)
def shiftright(self, num):
return BitMask(self > num)
def bit(self, num):
mask = 1 << num
return bool(self & mask)
def setbit(self, num):
mask = 1 << num
return BitMask(self | mask)
def zerobit (self, num):
mask = -(1 << num)
return BitMask(self & mask)
def listbits(self, start=0, end=-1):
end = end if end < 0 else end+2
return [int(c) for c in bin(self)[start+2:end]]