I need to calculate the prefix length of a IPv4 network mask. My first naive approach is
int calculate_prefix_length (uint32_t address) {
int set_bits;
for (set_bits = 0; address; address >>= 1) {
set_bits += address & 1;
}
return set_bits;
}
This is probably not very fast. is there a better solution?