CSC270 Python Operators for Simulation
--D. Thiebaut 10:09, 2 March 2012 (EST)
Source (Python V. 2.7)
# demonstration of Python operators for
# simulating boolean logic with 0s and 1s
#
def NOT( a ):
return 1-a
def NAND( a, b ):
return NOT( a & b )
def NOR( a, b ):
return NOT( a | b )
def XNOR( a, b ):
return NOT( a ^ b )
def decoder( msb, lsb ):
outputs = [0, 0, 0, 0]
index = msb*2 + lsb
outputs[ index ] = 1
return outputs
def mux( I0, I1, I2, I3, c1, c0 ):
index = c1 * 2 + c0
if index == 0: return I0
if index == 1: return I1
if index == 2: return I2
if index == 3: return I3
a = 1
b = 0
print "a = ", a
print "b = ", b , "\n"
print "AND: a & b = ", a & b, " a & a = ", a & a, "\n"
print "OR: a | b = ", a | b, " a | a = ", a | a, "\n"
print "XOR: a ^ b = ", a ^ b, " a ^ a = ", a ^ a, "\n"
print "NOT: NOT( a ) = ", NOT( a ), " NOT( b ) = ", NOT( b ), "\n"
print "NAND: NAND( a , b ) = ", NAND( a , b ), "\n"
print "NOR: NOR( a , b ) = ", NOR( a , b ), "\n"
print "XNOR: XNOR( a, b ) = ", XNOR( a, b ), "\n"
print "DECODER: decoder( a, b ) = ", decoder( a, b ), "\n"
print "MUX: mux( 1, 0, 1, 1, a, b ) = ", mux( 1, 0, 1, 1, a, b ), "\n"
Output
# OUTPUT:
a = 1
b = 0
AND: a & b = 0 a & a = 1
OR: a | b = 1 a | a = 1
XOR: a ^ b = 1 a ^ a = 0
NOT: NOT( a ) = 0 NOT( b ) = 1
NAND: NAND( a , b ) = 1
NOR: NOR( a , b ) = 0
XNOR: XNOR( a, b ) = 0
DECODER: decoder( a, b ) = [0, 0, 1, 0]
MUX: mux( 1, 0, 1, 1, a, b ) = 1