Thursday 14 June 2018

Cont the bits eigther signed number

#include
unsigned int countBits(char n)
{
   unsigned int count = 0;
   int len = sizeof(n) * 8;
   while (n && (len--))
   {
        if(n & 1)
          count++;
        n >>= 1;
    }
    return count;
}
/* Driver program*/
int main()
{
    int i = -4;
    printf("%d", countBits(i));
    return 0;
}


output: 6

No comments:

Post a Comment