2014-09-13 24 views
0

我不得不“&”两级98位长度的消息,但当我做输出包含比其他的数字1和0可能有人看到我搞乱:按位与输出数且0

print("The binaryCode of the input message is:", binaryCode) 
print("The gsBinary is:", gsBinary) 
print(len(gsBinary), len(binaryCode)) 

newStr = int(gsBinary) & int(binaryCode) 
print("The result of anding the gsBinary & binaryCode was:", newStr) 

这是电流输出时运行:

The binaryCode of the input message is: 11001111110010110010111000011110100100000110100011000011110011110100010000011001101110101110111011 

The gsBinary is: 11001111110010110010111001011101110110110011000011110111111001111011101111001110010011001011110010 

98 98 

The result of anding the gsBinary & binaryCode was: 11001111110010110010110999119920984020956932636701193212439574622352446504985730534177701743362082 

为什么打印中newstr长度98的字符串与比其它值1 & 0,如果两个输入串只包含1和0?

回答

7

你需要解释的字符串不是10个碱基的整数,但作为2基整数:

newinteger = int(gsBinary, 2) & int(binaryCode, 2) 

注意的第二个参数int(); 101被解释为整数5而不是101那样。

输出不是字符串;这是另外一个整数,所以你需要将其转换回字符串表示二进制:

newstr = format(newinteger, 'b') 

演示:

>>> binaryCode = '11001111110010110010111000011110100100000110100011000011110011110100010000011001101110101110111011' 
>>> gsBinary = '11001111110010110010111001011101110110110011000011110111111001111011101111001110010011001011110010' 
>>> newinteger = int(gsBinary, 2) & int(binaryCode, 2) 
>>> format(newinteger, 'b') 
'11001111110010110010111000011100100100000010000011000011110001110000000000001000000010001010110010' 
+0

谢谢它的工作! – user1234567878787 2014-09-13 12:27:12