This Python program reads two binary numbers (0 or 1) from the user and computes the results of the following logical operations:
XOR, OR, AND, NOR, NAND
def logical_operations(a: int, b: int) -> dict:
return {
"AND": a & b,
"OR": a | b,
"XOR": a ^ b,
"NAND": int(not (a & b)),
"NOR": int(not (a | b))
}
# Read inputs from user
a = int(input("Enter first binary number (0 or 1): "))
b = int(input("Enter second binary number (0 or 1): "))
# Validate input
if a not in [0, 1] or b not in [0, 1]:
print("Only 0 or 1 are allowed.")
else:
results = logical_operations(a, b)
for op, val in results.items():
print(f"{op}: {val}")
AND: 0
OR: 1
XOR: 1
NAND: 1
NOR: 0