In this thread is shown how to go though all the submasks of a given mask without extra space or time. I needed to do the same thing for the overmasks, but I did not find anything. Based on the formula mask ^ (~mask) = -1
, my brother gave me this code:
for (int over = (1 << n) - 1; over > 0; over = ((over - mask - 1) & ~mask) + mask) {
cout << over << " ";
}
It really works. But can I simplify it, so it could be easier remembered during the contest?