-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnandgame.py
44 lines (32 loc) · 814 Bytes
/
nandgame.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
def NAND(a, b):
if a:
if b:
return 0
return 1
print('NAND(1,0)=', NAND(1, 0))
def INV(a):
return NAND(a, a)
print('INV(1)=', INV(1))
def AND(a,b):
return INV(NAND(a, b))
def OR(a, b):
return NAND(INV(a), INV(b))
def XOR(a, b):
return AND(OR(a, b), NAND(a, b))
def HALF_ADD(a, b):
return AND(a, b), XOR(a, b)
h, l = HALF_ADD(1,1)
print('HALF_ADD(1,1)=', h, l)
def FULL_ADD(a, b, c):
h,l = HALF_ADD(a, b)
h1,l1 = HALF_ADD(l, c)
return OR(h1, h), l1
h,l = FULL_ADD(1,1,1)
print('FULL_ADD(1,1,1)=', h, l)
def MULTI_BIT_ADD(a, b, c):
s = [0]*len(a)
for i in range(len(a)):
c, s[i] = FULL_ADD(a[i], b[i], c)
return c, s
c, s = MULTI_BIT_ADD([0,1,1,0,0],[1,1,0,0,0],0)
print('MULTI_BIT_ADD([0,1,1,0,0],[1,1,0,0,0],0)=', c, s)