title: How can I get the position of bits
body: <p>I have a decimal number which I need to convert to binary and then find the position of one's in that binary representation.</p>
  
  <p>Input is 5 whose binary is <code>101</code> and Output should be </p>
  
  <pre><code>1
  3
  </code></pre>
  
  <p>Below is my code which only provides output as <code>2</code> instead I want to provide the position of one's in binary representation. How can I also get position of set bits starting from 1?</p>
  
  <pre><code>public static void main(String args[]) throws Exception {
      System.out.println(countBits(5));
  }
  
  private static int countBits(int number) {
      boolean flag = false;
  
      if (number &lt; 0) {
          flag = true;
          number = ~number;
      }
      int result = 0;
      while (number != 0) {
          result += number &amp; 1;
          number = number &gt;&gt; 1;
      }
      return flag ? (32 - result) : result;
  }
  </code></pre>