问题描述
如何在 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'