本文介紹了Python中的二進(jìn)制數(shù)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
如何在 Python 中添加、減去和比較二進(jìn)制數(shù)而不轉(zhuǎn)換為十進(jìn)制數(shù)?
How can I add, subtract, and compare binary numbers in Python without converting to decimal?
推薦答案
您可以使用 bin() 和 int() 在二進(jìn)制的字符串表示形式之間進(jìn)行轉(zhuǎn)換
You can convert between a string representation of the binary using bin() and int()
>>> bin(88)
'0b1011000'
>>> int('0b1011000', 2)
88
>>>
>>> a=int('01100000', 2)
>>> b=int('00100110', 2)
>>> bin(a & b)
'0b100000'
>>> bin(a | b)
'0b1100110'
>>> bin(a ^ b)
'0b1000110'
這篇關(guān)于Python中的二進(jìn)制數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!