Q:

C program to counter number of 1\'s in an integer number.

0

C program to counter number of 1's in an integer number.

This program will count total number of 1's in an integer number. Here we are counting total number of 1's in an integer number using a User Define Function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Counting number of 1's using C program

/*C program to count number of 1's in a number */
 
#include <stdio.h>
 
int count1s(unsigned int num)
{
    unsigned char i;
    int count=0;
    
    unsigned char totalBits=sizeof(num)*8;
    
 
    for(i=0;i< totalBits;i++)
    {
        if( num & (1<< i) )
            count++;
    }
 
    return count;
}

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

Output

    Total number of 1's are : 3

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C solved programs/examples on Bitwise Operators

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to reverse bits of an integer number.... >>
<< C program to swap two nibbles of a byte....