write a program to find out number of on bits in a number?

Answer Posted / krishna kanth

#include<stdio.h>
main()
{
int setbit=1;
int number=16;//for example
int numBitSet=0;
clrscr();

while(setbit<=number)//important and optimized condition
{

if(number&setbit)
numBitSet++;

setbit=setbit<<1;
}
printf("%d",numBitSet);
getch();
}

Is This Answer Correct ?    10 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

application attempts to perform an operation?

1496


What are the advantages of union?

628


What is the best organizational structure?

644


Can main () be called recursively?

631


write a program that types this pattern: 12345678987654321 12345678 87654321 1234567 7654321 123456 654321 12345 54321 1234 4321 123 321 12 21 1 1

3297






Are there constructors in c?

598


Does c have enums?

603


Array is an lvalue or not?

641


What is the importance of c in your views?

598


State the difference between x3 and x[3].

652


How to write a multi-statement macro?

626


Which header file is used for clrscr?

583


Is it possible to pass an entire structure to functions?

563


What happens if you free a pointer twice?

610


What is call by reference in functions?

569