Is there a way to sum up bit positions of an array? – StackOverflow
import numpy as np def count_bits(a: np.uint8, lens=4): return np.unpackbits(a[:, None], 1)[:, 8-lens:].sum(0) a = np.array([1, 2, 3], 'u1') count_bits(a)
array([0, 0, 2, 2], dtype=uint64)