Count 1 bits in integer - C Operator

C examples for Operator:Bit Operator

Description

Count 1 bits in integer

Demo Code

#include <stdio.h>

int bitcount(unsigned x);

int main(void){
    printf("%d\n", bitcount(011111));
    return 0;/* w  ww.  j a v  a 2 s .  c  o m*/
}

int bitcount(unsigned x)
{
    int b;

    for (b = 0; x != 0; x &= (x - 1))
        b++;
    return b;
}

Result


Related Tutorials