Count Set Bits (Population Count)

Easy C Optimization Asked at Qualcomm
Solve this problem

Count the number of 1 bits in a 32-bit word without iterating over all 32 positions. Used for counting active interrupt sources or occupied slots in a bitmap allocator.

Constraints

  • Signature: `int popcount32(uint32_t value)`.
  • Must not loop 32 times; use a divide-and-conquer or Kernighan approach.
  • No __builtin_popcount.
  • No lookup tables.
  • Must return 0 for input 0 and 32 for 0xFFFFFFFF.
Starter codeC
1#include <stdint.h>2 3int popcount32(uint32_t value) {4    // TODO: count the set bits5    return 0;6}