def AND(x_1, x_2, wieght=1, bias=1.5):
if x_1 + x_2 - bias > 0:
return 1
else: return 0
def OR(x_1, x_2, wieght=1, bias=0.5):
if x_1 + x_2 - bias > 0:
return 1
else: return 0
def NAND(x_1, x_2, wieght=1, bias=1.5):
if x_1 + x_2 - bias > 0:
return 0
else: return 1
def NOR(x_1, x_2, wieght=1, bias=0.5):
if x_1 + x_2 - bias > 0:
return 0
else: return 1
def XOR (x_1, x_2, wieght=1, bias_1=1.5, bias_2=0.5):
return AND(OR(x_1, x_2, wieght, bias_2), NAND(x_1, x_2, wieght, bias_1), wieght, bias_1)
print(XOR(0,0))
print(XOR(0,1))
print(XOR(1,0))
print(XOR(1,1))
>>
0
1
1
0
def XNOR (x_1, x_2, wieght=1, bias_1=1.5, bias_2=0.5):
return int(not(XOR(x_1, x_2, wieght, bias_1, bias_2)))
print(XNOR(0,0))
print(XNOR(0,1))
print(XNOR(1,0))
print(XNOR(1,1))
>>
1
0
0
1
def half_adder(x_1, x_2, wieght=1, bias_1=1.5, bias_2=0.5):
s=XOR (x_1, x_2, wieght, bias_1, bias_2)
c=AND(x_1, x_2, wieght, bias_1)
return x_1,x_2,s,c
print(half_adder(0,0))
print(half_adder(0,1))
print(half_adder(1,0))
print(half_adder(1,1))
>>
(0, 0, 0, 0)
(0, 1, 1, 0)
(1, 0, 1, 0)
(1, 1, 0, 1)
def full_adder(x_1, x_2, Cin, wieght=1, bias_1=1.5, bias_2=0.5):
P=XOR (x_1, x_2, wieght, bias_1, bias_2)
Q=AND(P, Cin, wieght, bias_1)
R=AND(x_1, x_2, wieght, bias_1)
S=XOR (P, Cin, wieght, bias_1, bias_2)
Cout=OR(Q, R, wieght, bias_2)
return x_1, x_2, P, Q, R, S, Cout
print(full_adder(0,0,0))
print(full_adder(0,1,0))
print(full_adder(1,0,0))
print(full_adder(1,1,0))
print(full_adder(0,0,1))
print(full_adder(0,1,1))
print(full_adder(1,0,1))
print(full_adder(1,1,1))
>>
(0, 0, 0, 0, 0, 0, 0)
(0, 1, 1, 0, 0, 1, 0)
(1, 0, 1, 0, 0, 1, 0)
(1, 1, 0, 0, 1, 0, 1)
(0, 0, 0, 0, 0, 1, 0)
(0, 1, 1, 1, 0, 0, 1)
(1, 0, 1, 1, 0, 0, 1)
(1, 1, 0, 0, 1, 1, 1)
import numpy as np
size=8
a=np.round((np.random.randint(0,2,size))).tolist()
b=np.round((np.random.randint(0,2,size))).tolist()
print(a, b)
## 이진수 더하기 함수
def adder(a,b, weight=1, bias_1=1.5, bias_2=0.5):
a.reverse()
b.reverse()
first=half_adder(a[0], b[0], weight, bias_1, bias_2)
Cin=first[3]
answer=[first[2]]
for i in range(1, len(a)):
next=full_adder(a[i], b[i], Cin, weight, bias_1, bias_2)
answer.append(next[6])
Cin=next[7]
if i == len(a)-1:
answer.append(next[7])
answer.reverse()
return answer
>>
[0, 0, 1, 0, 0, 1, 1, 0] [0, 1, 1, 1, 0, 0, 1, 0]
adder(a, b, weight=1, bias_1=1.5, bias_2=0.5)
>>
[1, 0, 0, 1, 1, 0, 0, 0]