Give a method to count the number of ones in a 32 bit number?

Answer Posted / jayaprakash

#include<stdio.h>
#include<conio.h>

main()
{
int i;
int n;
int count=0;
int j;
int res=0;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
for(j=15;j>=0;j--)
{ i=1;
i=i<<j;
res=i&n;
if(res!=0)
count++;
}
printf("\nNumber of ones is:%d",count);


getch();

}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

a parameter passed between a calling program and a called program a) variable b) constant c) argument d) all of the above

661


How a string is stored in c?

592


What is a function simple definition?

624


If I have a char * variable pointing to the name of a function ..

662


Write a program to reverse a given number in c?

603






Write a program to print ASCII code for a given digit.

692


Combinations of fibanocci prime series

1116


Explain what does it mean when a pointer is used in an if statement?

621


What is switch in c?

652


Why can arithmetic operations not be performed on void pointers?

594


Why c is called free form language?

575


How do I send escape sequences to control a terminal or other device?

618


Can the sizeof operator be used to tell the size of an array passed to a function?

622


What is an expression?

661


Explain how can I right-justify a string?

627