title: Number of bits to represent a number
body: <p>I'm trying to write a function to return the number of bits a positive integer less that the Javascript limit of (2^53)-1 is. However im being hit by precision problems, and want to avoid big integer libraries.</p>
  
  <p>Method 1:</p>
  
  <pre><code>function bitSize(num)
  {
  return Math.floor( Math.log(num) / Math.log(2) ) + 1;
  }
  
  Pass: bitSize( Math.pow(2, 16) -1 ) = 16
  Pass: bitSize( Math.pow(2, 16) ) = 17
  Fail (Should be 48): bitSize( Math.pow(2, 48) -1 ) = 49 
  Pass: bitSize( Math.pow(2, 48) ) = 49
  </code></pre>
  
  <p>Method 2:</p>
  
  <pre><code>function bitSize(num)
  {
  var count = 0;
  while(num &gt; 0)
  {
      num = num &gt;&gt; 1;
      count++;
  }
  return count;
  }
  
  Pass: bitSize( Math.pow(2, 16) -1 ) = 16
  Pass: bitSize( Math.pow(2, 16) ) = 17
  Fail (Should be 48): bitSize( Math.pow(2, 48) -1 ) = 1
  Fail (Should be 49): bitSize( Math.pow(2, 48) ) = 1
  </code></pre>
  
  <p>Both methods fail to precision issues I think.</p>
  
  <p>Can anyone suggest an alternative method that will work for numbers between 0 -> 2^53-1</p>
  
  <p>Thanks.</p>