TypeError:不支持&:“NoneType”和“BitVector”的操作数类型

2024-04-28 16:38:17 发布

您现在位置:Python中文网/ 问答频道 /正文

这是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)

对于上面的代码,我得到了错误

^{pr2}$

如何使我的函数将参数作为一个位向量(因为我认为这就是造成问题的原因)


Tags: 代码shiftcheckwithlefttempnumbitstring
1条回答
网友
1楼 · 发布于 2024-04-28 16:38:17

看起来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

如果您使用的是^{} package,则需要升级到3.1或更高版本(当前版本是3.4.4),因为该版本在BitVector.shift_left()和{}方法中添加了{}。在

从项目变更日志:

Version 3.1:

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

相关问题 更多 >