After solving this problem, I looked to some of other's code. In several codes, I found something like this (not exactly this, it's a simplified version of that):
int n = 10;
for(int i = 1; i<=n; i+=i& - i;)
{
cout << i << " ";
}
Output: 1 2 4 8
I also tried for different values of n and the program outputs powers of 2 less than or equal to n.
But how is it working? What's exactly meant by i+=i&-i ?
Thanks for your patience and insight.