basics · intermediate · ~15 min
Count set bits across an array of words.
Implement:
int bitset_count(const uint32_t *bs, int nwords);
Return the total number of set bits across a bitset stored as nwords 32-bit words.
A word array bs and its length nwords.
Total set bits across all words.
nwords==0 returns 0.
#include <stdint.h>
#include <stddef.h>
/* Total number of set bits across a bitset of nwords 32-bit words. */
int bitset_count(const uint32_t *bs,int nwords){ (void)bs;(void)nwords; return 0; }
Reading past nwords; summing values instead of set bits.
A bitset represents a set of small integers compactly.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.