Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What is the most efficient way to count the number of bits
which are set in a value?

Answer Posted / venkat1435

main()
{
int n,count=0;
printf("enter a number");
scanf("%d",&n);//enterd no.is 5
while(n>0)
{
count++;
n=n&n-1;//binary of n is 101
// binary of n-1 is 100
//n&n-1 is (i.e 101
&100 =100 )

}
printf("%d",count);
getch();
} output is 2(i.e 2 ones in 101)

Is This Answer Correct ?    31 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can you add pointers together? Why would you?

1071


What is the size of enum in bytes?

1036


Why can't I perform arithmetic on a void* pointer?

1045


What are pointers? Why are they used?

1113


What is volatile variable in c with example?

997


What is the difference between local variable and global variable in c?

1163


Is a house a shell structure?

1108


What is structure pointer in c?

989


How many types of operator or there in c?

1022


How are portions of a program disabled in demo versions?

1231


Give the rules for variable declaration?

1114


Explain what is the difference between a string and an array?

1101


What is nested structure with example?

1012


struct screen_pos{ int row, col } ;move_right(cursor)struct screen_pos *cursor;{ cursor.col++; } /* This statementhas a syntax error */What is the correct statement a) cursor.col = cursor.col + 1; b) col.cursor++; c) *cursor.col++; d) pointer

1235


What is the difference between class and object in c?

1088