Exemple #1
0
def full_adder(a: Bit, b: Bit, c: Bit) -> Adder:
    """

    :param a: bit 1
    :param b: bit 2
    :param c: bit 3


    :return Returns the sum and carry bits of adding three bits. [0] = sum, [1] = carry
    """
    xor_ab = xor_gate(a, b)

    sum = xor_gate(xor_ab, c)
    carry = or_gate(and_gate(a, b), and_gate(xor_ab, c))

    return Adder(sum, carry)
Exemple #2
0
def half_adder(a: Bit, b: Bit) -> Adder:
    """
    Sums two single bits

    :param a: bit 1
    :param b: bit 2
    :return: Returns the sum and carry bits of adding two bits. [0] = sum, [1] = carry
    """
    sum = xor_gate(a, b)
    carry = and_gate(a, b)

    return Adder(sum, carry)
Exemple #3
0
def nand_gate(a: Bit, b: Bit) -> Bit:
    return not_gate(and_gate(a, b))
Exemple #4
0
def xor_gate(a: Bit, b: Bit) -> Bit:
    return and_gate(nand_gate(a, b), or_gate(a, b))
Exemple #5
0
print()

assert transistor(0) == 0
assert transistor(1) == 1
print("Verified Transistor")

assert transistor_internals(0, 1, 0) == 0
assert transistor_internals(1, 1, 0) == 1
print("\t- Direct Transistor")

assert transistor_internals(0, 0, 1) == 1
assert transistor_internals(1, 0, 1) == 0
print("\t- Inverse Transistor")
print()

assert and_gate(0, 0) == 0
assert and_gate(0, 1) == 0
assert and_gate(1, 0) == 0
assert and_gate(1, 1) == 1
print("Verified Gate - And")

assert or_gate(0, 0) == 0
assert or_gate(0, 1) == 1
assert or_gate(1, 0) == 1
assert or_gate(1, 1) == 1
print("Verified Gate - Or")
print()

assert not_gate(0) == 1
assert not_gate(1) == 0
print("Verified Not Gates")