I want to find the number of bits necessary to represent an unsigned numpy integer (or each element in an array of integers) in binary, in the same way the python's int.bit_length() does, but it seems that numpy has no equivalent function.
For example:
>>> int(0b1000).bit_length()
4
>>> np.uint8(0b1000).bit_length()
AttributeError: 'numpy.uint8' object has no attribute 'bit_length'
Can anyone help me find the correct function? My current approach is to convert each array element to a python int to find the bit length, which seems like an awful option, for speed and clarity:
np.vectorize(lambda np_int: int(np_int).bit_length())

bit_length()determines a unique valueksuch that2**(k-1) <= abs(x) < 2**k. You could implement your own function to apply this to a Numpy integer as opposed to your conversion. The documentation link also offers a simple way to calculatekbut it has a limitation. Hope this helps!0b110) are continuous, but 5 (0b101) is discontinuous because there's a 0 between 1s.