-
Notifications
You must be signed in to change notification settings - Fork 0
/
gates.py
61 lines (53 loc) · 1.2 KB
/
gates.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'''
gates\n
type:function\n
name-format: logic_[name]\n
AND\n
OR\n
NOT\n
NAND\n
NOR\n
XOR\n
XNOR
'''
from LogicPy.main_functions import *
'''Logic AND'''
def logic_and(val1, val2):
if valid_value(val1,val2):
val1 , val2 = normalize_logic_values(val1, val2)
return(val1 and val2)
else:
raise ValueError("Invalid Values!")
'''Logic OR'''
def logic_or(val1, val2):
if valid_value(val1,val2):
val1 , val2 = normalize_logic_values(val1, val2)
return(val1 or val2)
else:
raise ValueError("Invalid Values!")
'''Logic NOT'''
def logic_not(val):
if valid_value(val):
val = normalize_logic_values(val)[0]
if val == 1:
return 0
else:
return 1
else:
raise ValueError("Invalid Values!")
'''Logic NAND'''
def logic_nand(val1, val2):
return logic_not(logic_and(val1, val2))
'''Logic NOR'''
def logic_nor(val1, val2):
return logic_not(logic_or(val1, val2))
'''Logic XOR'''
def logic_xor(val1, val2):
cp1 = logic_and(val1, logic_not(val2))
cp2 = logic_and(logic_not(val1), val2)
return(logic_or(cp1, cp2))
'''Logic XNOR'''
def logic_xnor(val1, val2):
cp1 = logic_nor(val1, val2)
cp2 = logic_and(val1, val2)
return(logic_or(cp1, cp2))