diff --git a/packages/binary/src/count.ts b/packages/binary/src/count.ts index 20f674d3c1..f511462da5 100644 --- a/packages/binary/src/count.ts +++ b/packages/binary/src/count.ts @@ -11,6 +11,26 @@ export const popCount: FnN = (x) => ( (((x + (x >>> 4)) & 0xf0f0f0f) * 0x1010101) >>> 24 ); +/** + * Returns number of set bits (1's) in the given array (index range). + * + * @param data - + * @param start - + * @param n - + */ +export const popCountArray = ( + data: Uint32Array, + start = 0, + n = data.length +) => { + let num = 0; + for (let end = start + n; start < end; start++) { + const x = data[start]; + x > 0 && (num += popCount(x)); + } + return num; +}; + /** * Returns number of bit changes between `x` and `y`. *