int popcount(unsigned long x) { int n = 0; while (x) { n += x & 1; x >>= 1; } return n; }