Urvah Shabbir
Urvah Shabbir

Reputation: 985

TypeError: unsupported operand type(s) for &: 'NoneType' and 'BitVector'

here is the python code.

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)

For the above code I get the error

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'

How can I make my function take parameter as a BitVector (since this is what i think is creating the problem

Upvotes: 1

Views: 591

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124090

It looks like the BitVector.shift_left() method returns None, presumably because the bit vector is mutated in place.

There is no need to re-assign num in this case, just use:

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

If you are using the BitVector package you'll need to upgrade to version 3.1 or newer (current release is 3.4.4), as that release added return self to the BitVector.shift_left() and BitVector.shift_right() methods.

From the project changelog:

Version 3.1:

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

Upvotes: 2

Related Questions