Example for Adders from combinational module


In [1]:
# Imports

from __future__ import print_function
from BinPy.combinational.combinational import *

Half Adder


In [2]:
print(HalfAdder.__doc__)


This Class implements Half Adder, Arithmetic sum of two bits and return its
    Sum and Carry
    Output: [CARRY, SUM]
    Example:
        >>> from BinPy import *
        >>> ha = HalfAdder(0, 1)
        >>> ha.output()
        [0, 1]

    

In [3]:
# Initializing the HalfAdder

# Input is of the form (bit_1, bit_2)
ha = HalfAdder(1, 1)

# Output of HalfAdder
# Output is of the form [ CARRY, SUM ]

print (ha.output())


[1, 0]

In [4]:
# Input changes ( index, value )

ha.set_input(1, 0)

# New Output of the BinaryAdder

print (ha.output())


[0, 1]

In [5]:
# Using Connectors as the input lines
# Take a Connector

conn_1 = Connector(1)
conn_2 = Connector(0)
conn_3 = Connector()

# Setting the input of the Half Adder to the Connector conn_1 and conn_2

ha.set_inputs(conn_1, conn_2)

# Set Carry Output of Binary Adder to Connector conn_3

ha.set_output(0, conn_3)

# Use this connector as the input to gate1

gate1 = NOT(conn_3)

# Output of the gate1
print (gate1.output())


1

In [6]:
# Change the value of the conn_2
conn_2.set_logic(0)

# Verify with the output of the HalfAdder
print (ha.output())


[0, 1]

Full Adder


In [7]:
print(FullAdder.__doc__)


This Class implements Full Adder, Arithmetic sum of three bits and
    return its Sum and Carry
    Output: [CARRY, SUM]
    Example:
        >>> from BinPy import *
        >>> fa = FullAdder(0, 1, 1)
        >>> fa.output()
        [1, 0]
    

In [8]:
a, b, ci, s, co = Connector(1), Connector(1), Connector(1), Connector(), Connector()

# Initializing full adder using connectors
fa = FullAdder(a, b, ci)

# Connect outputs
fa.set_output(0, co)
fa.set_output(1, s)

In [9]:
print (co.get_logic(), s.get_logic())


1 1