title: Count bits used in int
body: <p>If you have the binary number 10110 how can I get it to return 5? e.g a number that tells how many bits are used? There are some likewise examples listed below:</p>
  
  <ul>
  <li>101 should return 3</li>
  <li>000000011 should return 2</li>
  <li>11100 should return 5</li>
  <li>101010101 should return 9</li>
  </ul>
  
  <p>How can this be obtained the easiest way in Java? I have come up with the following method but can i be done faster:</p>
  
  <pre><code>public static int getBitLength(int value)
  {
      if (value == 0)
      {
          return 0;
      }
      int l = 1;
      if (value &gt;&gt;&gt; 16 &gt; 0) { value &gt;&gt;= 16; l += 16; }
      if (value &gt;&gt;&gt; 8 &gt; 0) { value &gt;&gt;= 8; l += 8; }
      if (value &gt;&gt;&gt; 4 &gt; 0) { value &gt;&gt;= 4; l += 4; }
      if (value &gt;&gt;&gt; 2 &gt; 0) { value &gt;&gt;= 2; l += 2; }
      if (value &gt;&gt;&gt; 1 &gt; 0) { value &gt;&gt;= 1; l += 1; }
      return l;
  }
  </code></pre>