Python Binary operations program
def takeinput(ct):
numtype = input(ct+": int or bin?")
num = input(ct+": please enter your number, must be a positive integer")
if numtype == "int":
num = int(num)
return num
def bintoint(s):
ct = 0
ans = 0
for i in s[::-1]:
if i == "1":
ans += 2**ct
ct += 1
return ans
def inttobin(num):
if num == 0:
return 0
ct = 0
ans = ""
while(2**ct <= num):
ct += 1
ct -= 1
while(ct >= 0):
if num >= 2**ct:
num -= 2**ct
ans += "1"
else:
ans += "0"
ct -= 1
return ans
num1 = takeinput("1")
num2 = takeinput("2")
if type(num1) == type("a"):
num1 = bintoint(num1)
if type(num2) == type("a"):
num2 = bintoint(num2)
print("decimal sum: "+str(num1+num2))
print("binary sum: "+str(inttobin(num1+num2)))
print("decimal difference: "+str(num1-num2))
if num1 >= num2:
print("binary difference: "+str(inttobin(num1-num2)))
else:
print("binary difference: -"+str(inttobin(num2-num1)))
print("decimal product: "+str(num1*num2))
print("binary product: "+str(inttobin(num1*num2)))
print("decimal quotient: "+str(num1//num2))
print("binary quotient: "+str(inttobin(num1//num2)))
print("decimal remainder: "+str(num1%num2))
print("binary remainder: "+str(inttobin(num1%num2)))