-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitVector.py
46 lines (34 loc) · 1.4 KB
/
BitVector.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
# Abdullah Arif
# Class to represent bit vector in Python
# Used for the PCY algorithm
import math
class BitVector:
posVector = b'\x80\x40\x20\x10\x08\x04\x02\x01' # holds arguments to target a bit in the byte vector
def __init__(self, size: float):
sz = math.ceil(size / 8) # store size of bit vector
self.bitVector = bytearray(sz)
# since each byte has 8 bits we have to translate position into a tuple
@staticmethod
def find_pos(pos: int) -> tuple:
return math.floor(pos / 8), pos % 8
def get_bit(self, pos: int) -> bool:
pos = BitVector.find_pos(pos)
check = self.posVector[pos[1]] & self.bitVector[pos[0]]
if check == 0:
return False
return True
# Flip value at position by using XOR
def flip_bit(self, pos: int):
pos = BitVector.find_pos(pos)
self.bitVector[pos[0]] ^= self.posVector[pos[1]]
# Set bit a position to true by using OR
def set_bit(self, pos: int):
pos = BitVector.find_pos(pos)
self.bitVector[pos[0]] |= self.posVector[pos[1]]
# Set bit a position to false by using AND
def reset_bit(self, pos: int):
pos = BitVector.find_pos(pos)
self.bitVector[pos[0]] &= ~self.posVector[pos[1]]
def print_vector(self):
i = int.from_bytes(self.bitVector, byteorder='big')
print("hex: " + "{0:x}".format(i))