Homework/Hacks

our homework we have decided for a decimal number to binary converter. You must use conditional statements within your code and have a input box for where the decimal number will go. This will give you a 2.7 out of 3 and you may add anything else to the code to get above a 2.7.

Below is an example of decimal number to binary converter which you can use as a starting template.

#    strs = ""
#    while num:
#        # if (num & 1) = 1
#        if (num & 1):
#            strs += "1"
      # if (num & 1) = 0
#        else:
#            strs += "0"
#        # right shift by 1
#        num >>= 1
#    return strs
 
# function to reverse the string
#def reverse(strs):
#    print(strs[::-1])
# Driver Code
#num = 14
#print("Binary of num 19 is:", end=" ")
#reverse(DecimalToBinary(num))

NumInput = int(input("What decimal number would you like to convert to binary?"))

#x = int(NumInput)

def DecBinConversion(x):
    for i in range(31, -1, -1):
        k = x >> i 
        if (k & 1):
            print("1", end="")
        else: 
            print("0", end="")
x = NumInput
DecBinConversion(x)
00000000000000000000000000000101