What advantage does this function have over a more “conventional” implementation?
int foo (int n)
{
int k = 0;
int t;
while (t = n & -n) {
++k;
n &= ~t;
}
return k;
}
— ADVERTISMENT—
—Advertise Here—
Home » EQ #2
What advantage does this function have over a more “conventional” implementation?
int foo (int n)
{
int k = 0;
int t;
while (t = n & -n) {
++k;
n &= ~t;
}
return k;
}
On average, this function is faster. It only iterates as many times as there are ones in the argument. Conventional algorithms usually iterate over all of the bits in the argument.
Keep up-to-date with our FREE Weekly Newsletter!
Don't miss out on upcoming issues of Circuit Cellar.
— ADVERTISMENT—
—Advertise Here—
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.