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 / bryan w

Question needs clarification. What platform? There are
bitwise trick that are optimal for various platforms, but
you need to know if the value is 16 bit, 32 bit, 64 bit, 128
bit, or something else entirely.

The earlier three examples are all incorrect for operating
on signed integers; if a negative value is presented the
code will fail.

The most efficient portable human readable answer is to loop
over sizeof type * CHAR_BITS times, shift left and add one
if the bit is set.

On some platforms the most efficient way is to use a bit of
assembly (such as the POPCNT instruction if available) to
perform the operation for you.

Without either of those, there are simple classic algorithms
of &, |, and ^ to accumulate the bits and sum them. They
need to be adjusted to match the architecture's number of
bits. These routines may be inefficient on modern PCs with
long pipelines or out-of-order processing cores.

Is This Answer Correct ?    1 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Write a program that takes a 5 digit number and calculates 2 power that number and prints it(should not use big integers and exponential functions)

6876


Synonymous with pointer array a) character array b) ragged array c) multiple array d) none

1071


What is the size of empty structure in c?

1086


What are the different file extensions involved when programming in C?

1285


What is dynamic dispatch in c++?

1066


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

1171


When should a type cast be used?

1018


What are pragmas and what are they good for?

1001


Why C language is a procedural language?

1039


how to execute a program using if else condition and the output should enter number and the number is odd only...

2303


Explain what does the format %10.2 mean when included in a printf statement?

1365


What is use of null pointer in c?

1017


What is the purpose of scanf() and printf() functions?

1245


Explain how can a program be made to print the line number where an error occurs?

1220


Differentiate between a for loop and a while loop? What are it uses?

1188