Created
August 4, 2022 11:25
-
-
Save tusqasi/2b09c334e2ce2b906ddc88a6df4fb0f7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from itertools import permutations, combinations_with_replacement | |
def and_g(a: bool, b: bool) -> bool: | |
return a and b | |
def or_g(a: bool, b: bool) -> bool: | |
return a or b | |
def xor_g(a: bool, b: bool) -> bool: | |
return a ^ b | |
def xand_g(a: bool, b: bool) -> bool: | |
return not and_g(a, b) | |
def half_adder(a, b): | |
return [xor_g(a, b), and_g(a, b)] | |
# this is just to generate the tables | |
table = sorted( | |
list( | |
set(permutations([0, 1])).union( | |
set(combinations_with_replacement([0, 1], r=2), ))), | |
key=lambda x: x[1] + x[0], | |
) | |
# this code will run the half adder for each entry in table | |
for a, b in table: | |
addition = half_adder(a, b) | |
print(f"{a=}, {b=}: sum: {addition[0]} carry: {addition[1]}") | |
""" Output: | |
a=0, b=0: sum: 0 carry: 0 | |
a=0, b=1: sum: 1 carry: 0 | |
a=1, b=0: sum: 1 carry: 0 | |
a=1, b=1: sum: 0 carry: 1 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment