Skip to content

Commit

Permalink
feat(binary): add popCountArray()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 18, 2022
1 parent 09ce729 commit d59e0c4
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/binary/src/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*
Expand Down

0 comments on commit d59e0c4

Please sign in to comment.