What will be printed out as a result of the following code execution?
#include <iostream>

template<int T>
int TempFunc()
{
    return 1 + TempFunc<T & (T-1)>();
}

template<>
int TempFunc<0>()
{
    return 0;
}

int main()
{
    std::cout << TempFunc<13>() << TempFunc<5>();
}
Explanation
This is a template implementation of the function of counting the number of set bits in a number.

Operation x&(x-1) "nulls" the lowest nonzero digit. Visual explanation:
   x    == 01011000
 (x-1)  == 01010111
x&(x-1) == 01010000

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes