以下是python代码。
from BitVector import *
MX = BitVector(bitstring = '00011011')
MSB_check = BitVector(bitstring = '10000000')
def multiplication_logic(num):
num = num.shift_left(1) # left shift
MSB_num = num & MSB_check # AND num with 1000 0000 to get only MSB
if MSB_num.intValue() != 0:
num = num ^ MX #XOR with 00011011
return num
for indexOfOneInPoly2 in range (0,7):
if polynomial_2[indexOfOneInPoly2] == 1 and indexOfOneInPoly2 != 0:
for numberOfIndexTimes in range (0,indexOfOneInPoly2):
temp = multiplication_logic(polynomial_1)
print(temp)
polynomial_3 = polynomial_3 + temp
print(polynomial_3)对于上面的代码,我得到了错误
Traceback (most recent call last):
File "<pyshell#126>", line 4, in <module>
temp = multiplication_logic(polynomial_1)
File "<pyshell#124>", line 3, in multiplication_logic
MSB_num = num & MSB_check
TypeError: unsupported operand type(s) for &: 'NoneType' and 'BitVector'如何才能让我的函数接受参数作为BitVector (因为我认为这就是导致问题的原因
发布于 2015-09-23 19:07:39
它看起来像是BitVector.shift_left()方法返回None,大概是因为位向量发生了原地突变。
在这种情况下,不需要重新分配num,只需使用:
def multiplication_logic(num):
num.shift_left(1)
MSB_num = num & MSB_check # AND num with 1000 0000 to get only MSB
if MSB_num != 0:
num = num ^ MX #XOR with 00011011
return num如果您使用的是BitVector package,则需要升级到3.1版或更高版本(当前版本为3.4.4),因为该版本在BitVector.shift_left()和BitVector.shift_right()方法中添加了return self。
在项目changelog中:
3.1版:
此版本包括:....非循环位移位方法现在返回self,以便可以链接它们;
https://stackoverflow.com/questions/32737875
复制相似问题