forked from satojkovic/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitarray.py
More file actions
22 lines (20 loc) · 725 Bytes
/
Copy pathbitarray.py
File metadata and controls
22 lines (20 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import array
class BitArray:
def __init__(self, bit_size):
int_size = bit_size >> 5 # number of 32 bit integers
if bit_size & 31:
int_size += 1
self.bit_array = array.array('L') # unsigned 32 bit integer
self.bit_array.extend((0,) * int_size)
# Set an integer with the bit at 'bit_num' to 1
def set(self, bit_num):
record = bit_num >> 5
offset = bit_num & 31
mask = 1 << offset
self.bit_array[record] |= mask
# Returns non-zero result if the bit at 'bit_num' is set to 1
def get(self, bit_num):
record = bit_num >> 5
offset = bit_num & 31
mask = 1 << offset
return self.bit_array[record] & mask