Python 3.7.4 (default, Aug 13 2019, 20:35:49)
[GCC 7.3.0] :: Anaconda, Inc. on linux
>>> np.version.version
'1.17.2'
I am maintaining integers as a bit array in order to manipulate at bit level algorithmically. I am using a fairly standard way to convert from the bit array to an integer and then view in decimal format.
def decimal(binaryValue):
decimalValue = 0
for bit in binaryValue:
decimalValue = (decimalValue << 1) | bit
print(decimalValue) #For testing
return decimalValue
I was weirdly receiving negative integers randomly for certain arrays 64-bit or larger. I realised after some hair pulling and crazy debugging that this happens when I use a NumPy array. No issue with regular lists. Here is a specific example:
>>> b = [1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1]
>>> decimal(b)
1
3
7
...
4418570559336253839
8837141118672507678
17674282237345015357
17674282237345015357
>>> import numpy as np
>>> b_np = np.array(b)
>>> b_np
array([1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1])
>>> decimal(b_np)
1
3
7
...
4418570559336253839
8837141118672507678
-772461836364536259
-772461836364536259
>>> np.binary_repr(decimal(b))
'1111010101000111101010100000110101110000111100100010011000111101'
>>> np.binary_repr(decimal(b_np))
'-101010111000010101011111001010001111000011011101100111000011'
As you can see, with the numpy array representation, something happens on the last bit evaluation. If I convert the numpy array back to a list, then I now get a negative number! Very very weird. Something happens in the numpy space. But c is identical to b.
>>> c = list(b_np)
>>> c
[1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1]
>>> decimal(c)
1
3
7
...
4418570559336253839
8837141118672507678
-772461836364536259
-772461836364536259
>>>np_binary_rep(decimal(c))
'-101010111000010101011111001010001111000011011101100111000011'
Simple checks:
>>> len(b)
64
>>> len(b_np)
64
>>> len(c)
64
>>> b == c
True
>>> b == b_np
array([ True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True])
What is going on? Obviously a 64-bit issue, but can't see where. Thanks.