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

How are structure passing and returning implemented?

594


What is the use of static variable in c?

597


what is ur strangth & weekness

1826


Difference between linking and loading?

699


What is the scope of global variable in c?

560






how can I convert a string to a number?

601


How many levels of indirection in pointers can you have in a single declaration?

601


How do I swap bytes?

634


What is a null pointer assignment error? What are bus errors, memory faults, and core dumps?

909


What is the right way to use errno?

625


What is New modifiers?

674


What does c mean before a date?

596


Explain what are the different file extensions involved when programming in c?

639


Explain how can I right-justify a string?

627


What is the full form of getch?

587