0

I have being try to understand why there is the output not all the elements are print only first and second are print individually but not the third it starts the combination not the single third as still in the second loop when j=3 for which (counter &(1<<j)) is also true.so here is the code:

for (int counter = 1; counter < opsize; counter++)
    {
        for (int j = 0; j < n; j++)
        {
            /* Check if jth bit in the counter is set
                If set then print jth element from arr[] */
            if (counter & (1<<j))
                cout << arr[j] << " ";
        }
        cout << endl;
    }
}
Chinna
  • 1
  • 1
  • 2
    Would you mind posting a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) including missing parts of code (including declaration and initialization of `opsize` and `arr`), expected output (and actual output)? – MikeCAT Jan 08 '21 at 13:25
  • Here is [an example run](https://wandbox.org/permlink/EtNlP0DQNf3xFiIw). – MikeCAT Jan 08 '21 at 13:28
  • When `counter < 8 , j = 3`, `(counter &(1< – MikeCAT Jan 08 '21 at 13:31
  • Your description is very vague. What do you mean by the "first", "second", and "third"? – molbdnilo Jan 08 '21 at 13:38
  • Is not clear what you want to achieve, I suggest you do add the current output and expected Output – Federico Baù Jan 08 '21 at 14:06

0 Answers0