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) {
    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.

Don't miss out on upcoming issues of Circuit Cellar. Subscribe today!

Note: We’ve made the October 2017 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.