十進制轉換為二進制 python實現


 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     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM