title: Why is bitwise operator needed in this powerset generator?
body: <p>I am currently following MITx's 6.00.2x, and we are asked to come up with a variant of power set generator of the one at the bottom.</p>
  
  <p>But before I can work on the variant, I do not even understand what's going on with the given generator. Specifically:</p>
  
  <ol>
  <li>What does <code>(i &gt;&gt; j) % 2 == 1</code>, and in fact the whole <code>for j in range(N):</code> block do? I understand that <code>i &gt;&gt; j</code> shifts
  the binary of <code>i</code> by <code>j</code>, then returns the decimal representation of
  that shifted binary number. But I have absolutely no clue why binary
  is even needed in a powerset generator in the first place, let alone
  the necessity of this conditional.</li>
  <li><p>I understand that for any given set A a cardinality n, the
  cardinality of its powerset is 2**n - because for every subset of A
  every member is either in or not, and we repeat that for n times.</p>
  
  <p>Is that what <code>for i in range(2**N):</code> is doing? i.e. going over 2**n subsets and either include or not include any given member of the set?</p></li>
  </ol>
  
  <p>I tried running it with <code>items=['apple,'banana','orange']</code> and <code>items=[1,2,3]</code>, and both returned an empty list, which makes it all the more confusing.</p>
  
  <pre><code>def powerSet(items):
      # generate all combinations of N items, items is a Python list
      N = len(items)
      # enumerate the 2**N possible combinations
      for i in range(2**N):
          combo = []
          for j in range(N):
              # test bit jth of integer i
              if (i &gt;&gt; j) % 2 == 1:
                  combo.append(items[j])
          return combo
  </code></pre>