-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalu.py
More file actions
40 lines (27 loc) · 746 Bytes
/
Copy pathalu.py
File metadata and controls
40 lines (27 loc) · 746 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
35
36
37
38
39
40
def b_and(a,b):
out = ""
for i in range(len(a)):
out += str(int(a[i]) & int(b[i]))
return out
def b_or(a,b):
out = ""
for i in range(len(a)):
out += str(int(a[i]) | int(b[i]))
return out
def b_not(a):
out = ""
for i in range(len(a)):
out+= "1" if a[i]=="0" else "0"
return out
def alu(x,y,zx,nx,zy,ny,f,no):
if zx: x = "0"*len(x)
if nx: x = b_not(x)
if zy: y = "0"*len(y)
if ny: y = b_not(y)
if f: out = bin(int(x,2) + int(y,2))[2:].zfill(len(x))[-len(x):]
else: out = b_and(x,y)
if no: out = b_not(out)
if len(out) > len(x): out = out[1:]
zr = "1" if int(out,2) == 0 else "0"
ng = "1" if out[0] == "1" else "0"
return out,zr,ng