gpt4 book ai didi

python - 类型错误 : unsupported operand type(s) for &: 'NoneType' and 'BitVector'

转载 作者:行者123 更新时间:2023-12-01 04:28:38 40 4
gpt4 key购买 nike

这是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 (因为我认为这是造成问题的原因

最佳答案

看起来 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),因为该版本将 return self 添加到 BitVector.shift_left()BitVector.shift_right() 方法。

来自项目变更日志:

Version 3.1:

This version includes: [....] (3) The non-circular bit shift methods now return self so that they can be chained;

关于python - 类型错误 : unsupported operand type(s) for &: 'NoneType' and 'BitVector' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32737875/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com