# A 2-to-1 multiplexer examples along with a function to print out # the complete truth table for the 2-to-1 multiplexer # # Authors: David Kauchak and Rett Bull # Date: 3/3/15 # # The code below requires: # 1. That you have the included the starter code for assignment 5 # 2. That OrGate2 to be defined class TwoToOneMultiplexer: def __init__(self, input0, input1, control, output): control_negate = Node(0) NotGate1(control, control_negate) and_out1 = Node(0) AndGate2(input0, control_negate, and_out1) and_out2 = Node(0) AndGate2(input1, control, and_out2) OrGate2(and_out1, and_out2, output) def testTwoToOne(): in1 = Node(0) in2 = Node(0) select = Node(0) output = Node(0) TwoToOneMultiplexer(in1, in2, select, output) print "in0".rjust(4) + "in1".rjust(4) + "sel".rjust(4) + "out".rjust(4) for val in range(8): i = val % 2 # get the first bit j = (val / 2) % 2 # get the second bit c = (val / 4) % 2 # get the third bit in1.set_state(i) in2.set_state(j) select.set_state(c) print str(i).rjust(4) + str(j).rjust(4) + \ str(c).rjust(4) + str(output.get_state()).rjust(4)