A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C program to counter number of 1\'s in an integer number.
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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now