forked from Firkraag/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_counter.py
More file actions
21 lines (21 loc) · 604 Bytes
/
Copy pathbinary_counter.py
File metadata and controls
21 lines (21 loc) · 604 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class binary_counter(list):
def __init__(self, size):
self.leftmost_1 = -1
list.__init__(self, [0] * size)
def increment(self):
i = 0
while i < len(self) and self[i] == 1:
self[i] = 0
i = i + 1
if i < len(self):
self[i] = 1
if self.leftmost_1 < i:
self.leftmost_1 = i
else:
self.leftmost_1 = -1
def reset(self):
for i in range(0, self.leftmost_1 + 1):
self[i] = 0
self.leftmost_1 = -1
def print_bits(self):
print self[::-1]