Count Bits with Value 0

From Software Engineers Wiki
Jump to: navigation, search

Write a function that counts number of bits with bit value 0.

Answer

#include <limits.h>

int count_bit_0(unsigned int n)
{
        int count;

        for (count = 0; n != UINT_MAX; ++count)
                n |= n + 1;

        return count;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox