-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryAddition.py
More file actions
34 lines (26 loc) · 859 Bytes
/
binaryAddition.py
File metadata and controls
34 lines (26 loc) · 859 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
26
27
28
29
30
31
32
33
34
from itertools import zip_longest
# states
p0c0 = 0, {}
p1c0 = 1, {}
p0c1 = 0, {}
p1c1 = 1, {}
# transitions between states
p0c0[1].update({(0, 0): p0c0, (1, 0): p1c0, (0, 1): p1c0, (1, 1): p0c1})
p1c0[1].update({(0, 0): p0c0, (1, 0): p1c0, (0, 1): p1c0, (1, 1): p0c1})
p0c1[1].update({(0, 0): p1c0, (1, 0): p0c1, (0, 1): p0c1, (1, 1): p1c1})
p1c1[1].update({(0, 0): p1c0, (1, 0): p0c1, (0, 1): p0c1, (1, 1): p1c1})
def add(x, y):
x = map(int, reversed(x))
y = map(int, reversed(y))
z = []
# simulate automaton
value, transition = p0c0
for r, s in zip_longest(x, y, fillvalue=0):
value, transition = transition[r, s]
z.append(value)
# handle carry
z.append(transition[0, 0][0])
return ''.join(map(str, reversed(z)))
#RUN
add('1100100100100', '100100011000')
bin(0b1100100100100 + 0b100100011000)