unsigned int num_ones(unsigned int n) { unsigned int num = 0; while(n) { num += (n&1); n >>= 1; } return num; }