Test Your EQ (Engineering Quotient)

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.

Note: We’ve made the May 2020 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.


Would you like to write for Circuit Cellar? We are always accepting articles/posts from the technical community. Get in touch with us and let's discuss your ideas.

— ADVERTISMENT—

Advertise Here

Supporting Companies

Upcoming Events

Copyright © 2021 KCK Media Corp.

EQ #2

by Circuit Cellar Staff time to read: <1 min