Python中的二进制数 [英] Binary numbers in Python
本文介绍了Python中的二进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Python 中添加、减去和比较二进制数而不转换为十进制数?
How can I add, subtract, and compare binary numbers in Python without converting to decimal?
推荐答案
您可以使用 bin() 和 int() 在二进制的字符串表示形式之间进行转换
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'
这篇关于Python中的二进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文