title: Recursion vs bitmasking for getting all combinations of vector elements
body: <p>While practicing for programming competitions (like ACM, Code Jam, etc) I've met some problems that require me to generate all possible combinations of some vector elements.</p>
  
  <p>Let's say that I have the vector {1,2,3}, I'd need to generate the following combinations (order is not important) :</p>
  
  <pre><code>1
  2
  3
  1 2
  1 3
  2 3
  1 2 3
  </code></pre>
  
  <p>So far I've done it with the following code : </p>
  
  <pre><code>void getCombinations(int a)
  {
      printCombination();
      for(int j=a;j&lt;vec.size();j++)
      {
          combination.pb(vec.at(j));
          getCombinations(j+1);
          combination.pop_back();
      }
  }
  </code></pre>
  
  <p>Calling getCombinations(0); does the job for me. But is there a better (faster) way? I've recently heard of bitmasking. As I understood it's simply for all numbers between 1 and 2^N-1 I turn that number into a binary where the 1s and 0s would represent whether or not that element is included in the combinations.</p>
  
  <p>How do I implement this efficiently though? If I turn every number into binary the standard way (by dividing with 2 all the time) and then check all the digits, it seems to waste a lot of time. Is there any faster way? Should I keep on using the recursion (unless I run into some big numbers where recursion can't do the job (stack limit))?</p>