diff --git a/sdrbase/util/popcount.h b/sdrbase/util/popcount.h index 974a5b0b1..79cc62838 100644 --- a/sdrbase/util/popcount.h +++ b/sdrbase/util/popcount.h @@ -31,11 +31,17 @@ #include #define popcount __popcnt #else -static int popcount(int in) +// https://en.cppreference.com/cpp/numeric/popcount +// std::popcount requires an unsigned integer type. +// +// Brian Kernighan's method: +// https://graphics.stanford.edu/~seander/bithacks.html +static int popcount(unsigned int in) { - int cnt = 0; - for(int i = 0; i < 32; i++) - cnt += (in >> i) & 1; + for (int cnt = 0; in; cnt++) + { + in &= in - 1; // clear the least significant bit set + } return cnt; } #endif