1 #原碼 補碼 反碼 2 3 def Code(number): 4 5 Bin = bin(number)[2:] 6 7 return Bin 8 9 10 def main(): 11 12 while True: 13 14 number = eval(input("請輸入:")) 15 16 if number>0: 17 18 BinCode = Code(number) 19 20 compCode = antiCode = BinCode 21 22 else: 23 24 antiCode = Code(2**len(BinCode)-1-abs(number))#小於0的反碼 25 26 compCode = Code(2**len(BinCode)-abs(number)) #小於0的補碼 27 28 print("原碼:{}\n反碼:{}\n補碼:{}\n".format(BinCode,antiCode,compCode)) 29 30 31 main() 32 33 34 35
1 #原碼 補碼 反碼 2 3 def Code(number): 4 5 Bin = bin(number)[2:] 6 7 return Bin 8 9 10 def main(): 11 12 while True: 13 14 number = eval(input("請輸入:")) 15 16 if number>0: 17 18 BinCode = Code(number) 19 20 compCode = antiCode = BinCode 21 22 else: 23 24 antiCode = Code(2**len(BinCode)-1-abs(number))#小於0的反碼 25 26 compCode = Code(2**len(BinCode)-abs(number)) #小於0的補碼 27 28 print("原碼:{}\n反碼:{}\n補碼:{}\n".format(BinCode,antiCode,compCode)) 29 30 31 main() 32 33 34 35
