c/c++でbit length計算

int bitlen(int n)
{
  int count=0;
  if(n>0xffff)
  {
	  count+=16;
	  n>>=16;
  }
  if(n>0xff)
  {
	  count+=8;
	  n>>=8;
  }
 if(n>0xf)
  {
	  count+=4;
	  n>>=4;
  }
  if(n>0x7)
  {
	  count+=2;
	  n>>=2;
  }
  if(n>0x3)
  {
	  count+=1;
	  n>>=1;
  }
    if(n>0x1)
  {
	  count+=1;
	  n>>=1;
  }
   return count+n;
}

Development

Posted by arkgame