Created
November 7, 2014 19:35
-
-
Save mouseroot/319d87b3c1ae4498e0f7 to your computer and use it in GitHub Desktop.
Gates Class With Examples (HalfAdder/FullAdder)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LogicGates: | |
@staticmethod | |
def AND(A,B): | |
if A == 1 and B == 1: | |
return 1 | |
else: | |
return 0 | |
@staticmethod | |
def OR(A,B): | |
if A == 1 or B == 1: | |
return 1 | |
elif A == 0 and B == 0: | |
return 0 | |
@staticmethod | |
def NOR(A,B): | |
A = LogicGates.NOT(A) | |
B = LogicGates.NOT(B) | |
if A == 1 or B == 1: | |
return 1 | |
elif A == 0 and B == 0: | |
return 0 | |
@staticmethod | |
def NAND(A,B): | |
A = LogicGates.NOT(A) | |
B = LogicGates.NOT(B) | |
if A == 1 and B == 1: | |
return 0 | |
elif A == 0 or B == 0: | |
return 1 | |
@staticmethod | |
def XOR(A,B): | |
if A == 1 and B == 0 or A == 0 and B == 1: | |
return 1 | |
if A == 0 and B == 0: | |
return 0 | |
if A == 1 and B == 1: | |
return 0 | |
@staticmethod | |
def NOT(A): | |
if A == 1: | |
return 0 | |
elif A == 0: | |
return 1 | |
def HalfAdder(A,B): | |
S = LogicGates.XOR(A,B) | |
C = LogicGates.AND(A,B) | |
return (S,C) | |
def FullAdder(A,B,C): | |
ha0 = HalfAdder(A,B) | |
ha1 = HalfAdder(ha0[0],C) | |
res = LogicGates.OR(ha0[1],ha1[1]) | |
return (res,ha1[1]) | |
def main(): | |
print "Logic Gates test" | |
res0 = LogicGates.OR(1,0) | |
res1 = LogicGates.AND(1,1) | |
res2 = LogicGates.NOT(0) | |
res3 = LogicGates.NAND(1,0) | |
res4 = LogicGates.NOR(0,0) | |
res5 = LogicGates.XOR(1,0) | |
print "Results:" | |
for k,v in enumerate([res0,res1,res2,res3,res4,res5]): | |
print "res%d" % k,"->",v | |
print "" | |
print "Half Adder" | |
ha = HalfAdder(1,1) | |
print "Sum:",ha[0] | |
print "Carry:",ha[1] | |
print "" | |
print "Full Adder" | |
fa = FullAdder(1,1,0) | |
Sum,Carry = fa | |
print "Sum:",Sum | |
print "Carry:",Carry | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment