-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalu.py
37 lines (25 loc) · 783 Bytes
/
alu.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
def op_add(val1: int, val2: int):
return val1 + val2
def op_subtract(val1: int, val2: int):
return val1 - val2
def op_and(val1: int, val2: int):
return val1 & val2
def op_or(val1: int, val2: int):
return val1 | val2
def op_not(val1: int):
return ~val1
def op_xor(val1: int, val2: int):
return val1 ^ val2
def interpret(opcode: int, val1: int, val2: int):
if opcode == 0b0000_0001:
return op_add(val1, val2)
elif opcode == 0b0000_0010:
return op_subtract(val1, val2)
elif opcode == 0b0000_0011:
return op_and(val1, val2)
elif opcode == 0b0000_0100:
return op_or(val1, val2)
elif opcode == 0b0000_0101:
return op_not(val1)
elif opcode == 0b0000_0110:
return op_xor(val1, val2)