title: Which bit is on for an integer in Java
body: <p>I have written this code to check which bits are on of an Integer (if represented in binary) in Java:</p>
  
  <pre><code>public static List&lt;String&gt; list(int val)
  {
      List&lt;String&gt; dummyList = new ArrayList&lt;String&gt;();
  
      int bit = 1;
      int x;
  
      for(int i=0; i&lt;32; i++)
      {
          x = bit;
          if((x&amp;val)!=0)
              dummyList.add(String.valueOf(i+1));
          bit = bit &lt;&lt; 1;
      }
  
      return dummyList;
  }
  </code></pre>
  
  <p>The above written code works fine. But it has a loop which runs 32 times (In Java integer is 32 bit long). I want to minimize this complexity. Please share the better solution. Thanks in advance.</p>