/*C program to count number of 1's in a number */
 
#include <stdio.h>
 
int count1s(unsigned int num);

int main()
{
    unsigned int data = 0x58; // if 0xff  -> 8
    printf("\nTotal number of 1's are : %d\n",count1s(data));
 
    return 0;
}

int count1s(unsigned int num)
{
    unsigned char i;
    int count=0;
    
    unsigned char totalBits=8;// or sizeof(num)*8;// a & (1<<i)) !=0
    
 
    for(i=0;i< totalBits;i++)
    {
        if( num & (1<< i) ) // 001 & 001 then 1 .. if 8 then 11111111 so 8
            count++;
    }
 
    return count;
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: