Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
What is the most efficient way to count the number of bits which are set in an integer?
What is the most efficient way to count the number of bits which are set in an integer?
✍: Guest
Many ``bit-fiddling'' problems like this one can be sped up and streamlined using lookup tables (but see question 20.13). Here is a little function which computes the number of bits in a value, 4 bits at a time:
static int bitcounts[] =
{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
int bitcount(unsigned int u)
{
int n = 0;
for(; u != 0; u >>= 4)
n += bitcounts[u & 0x0f];
return n;
}
2015-02-11, 1089👍, 0💬
Popular Posts:
What Is the "@SuiteClasses" Annotation? "@SuiteClasses" is a class annotation defined in JUnit 4.4 i...
Can Two Forms Be Nested? - XHTML 1.0 Tutorials - Understanding Forms and Input Fields Can two forms ...
How To Enter Boolean Values in SQL Statements? - MySQL FAQs - Introduction to SQL Basics If you want...
Can two catch blocks be executed? No, once the proper catch section is executed the control goes fin...
Can two catch blocks be executed? No, once the proper catch section is executed the control goes fin...