I am trying to convert several masks (boolean arrays) to a bitmask with numpy, while it theoretically works, I feel like I am performing too many operations.
For example, to create a bitmask, I use:
import numpy as np flags = [ np.array([True, False, False]), np.array([False, True, False]), np.array([False, True, False]) ] flag_bits = np.zeros(3, dtype=np.int8) for idx, flag in enumerate(flags): flag_bits += flag.astype(np.int8) << idx
Which gives me the expected "bit mask":
>>> flag_bits array([1, 6, 0], dtype=int8) >>> [np.binary_repr(bit, width=7) for bit in flag_bits] ['0000001', '0000110', '0000000']
However, I feel that especially casting int8 and adding with the flag_bits array flag_bits too complicated. So I wanted to ask if there is any NumPy function that I missed that could be used to create such an array of "bitmasks"?
Note. I call an external function that expects such a bitmask, otherwise I would stick with boolean arrays.
source share