#### A decimal to binary converter (utilizing "two's complement)#### Made by Anthony Sinnott#### Version 2.1 - Supports negitive numbers, has cleaner code, and is fully commented # define method to convert possitive valuedef posbincon(value): # detect if the value equals zero or not if value == 0: binary = str(value) return binary else: binary=[] # evaluate each digit to determine if it is true or false while(value > 0): if value&1: binary.append("1") else: binary.append("0") # go on to next didgit (right-bitshift) value = value>>1 # convert list to string and reverse it binary = "".join(binary)[::-1] return binary# define method to convert negitive valuedef negbincon(value): # binary is the possitive output binary = posbincon(value) # lenbin is the length of the possitive output lenbin = len(binary) # conbin is the mechinism used to stop lines 30-32 conbin = 0 while conbin == 0: # if possitive output's current holding number is one, conbin is activated if binary[lenbin-1] == "1": conbin = 1 # this is to determine one numbers should and shouldn't be rotated lenbin = lenbin - 1 # notuse contains any numbers that won't be rotated, binary is the opposite notuse = binary[lenbin:] binary = binary[:lenbin] # binary2 will contain the new (rotated) set of numbers binary2 = "" # each digit is evaluated, 0s converted to 1s, 1s to 0s while(binary != ""): if binary[0] == "0": binary2 = binary2 + "1" if binary[0] == "1": binary2 = binary2 + "0" binary = binary[1:] # appends the numbers not rotated to the ones that were binary = binary2 + notuse return binary# small introductionprint "Welcome to the Ultimate, low budget binary to decimal converter"# the two's complement is a method of representing both negitive and possitive numbers in binary formatprint "Now including Two's Complement"print ""# quit is the mechanism used to stop the programquit = 0while quit == 0: # allows the user to enter the value to be converted value = input("Enter the number here:> ") # saves the value in string format decimal = str(value) # gives appropriate output depending on weither the value is possitive or negitive if value >= 0: print "The binary value of " + decimal + " is " + posbincon(value) else: value = -value print "The binary value of " + decimal + " is " + negbincon(value) print "" # asks the user if they want to continue to use the program quit = input("Quit, yes or no? 1 or 0:> ") print ""#### Program produce by Anthony Sinnott using a template made by Spencer######## How the "two's complement" conversion works:#### Numbers are reversed#### 1 is added to the right-most digit######## Shortcut used in this program:#### Values including and beyond the rightmost 1 are held#### All other values are reversed######## For more information visit:#### http://en.wikipedia.org/wiki/Two's_complement######## Thank you for supporting this program :)