在Python二进制数 [英] Binary numbers in Python
本文介绍了在Python二进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能加,减,和比较Python中的二进制数,无须转换为十进制?
How can I add, subtract, and compare binary numbers in Python without converting to decimal?
推荐答案
您可以将字符串重新二进制的presentation之间使用斌()和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屋!
查看全文