title: What is the efficient way of calculating sum of Bitwise AND of all subsets of an array in Java?
body: <p>For [1, 2, 3], all possible subsets are {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}</p>
  
  <p>The sum of AND of these subsets are, 1 + 2 + 3 + 0 + 1 + 2 + 0 = 9.</p>
  
  <p>this is what i have tried:</p>
  
  <pre><code>public class Main
  {
  static void printSubsets(int set[])
  {
      int n = set.length;
      int total=0;
  
      for (int i = 0; i &lt; (1&lt;&lt;n); i++)
      {
          int sum=1;
  
          for (int j = 0; j &lt; n; j++)
  
              if ((i &amp; (1 &lt;&lt; j)) &gt; 0)
  
                  sum = sum &amp; set[j];
  
                  total= total+sum;
  
  
      }
      System.out.print(total);
  }
  
  public static void main(String[] args)
  {
      int set[] = {1, 2, 3};
      printSubsets(set);
  }
  }
  </code></pre>
  
  <p>but its not giving correct output</p>