/**
* natural log method.
* Calculate how many bits wide a number is,
* i.e. position of highest 1 bit.
* @return p where 2**p is first power of two >= n.
* e.g. binary 0001_0101 -> 5, 0xffffffff -> 32,
* 0 -> 0, 1 -> 1, 2 -> 2, 3 -> 2, 4 -> 3
*/
public static int widthInBits1(int n )
{
if ( n < 0 )
{
return 32;
}
if ( n > 0x3fffffff )
{
return 31;
}
return(int)Math.ceil( Math.log( n+1 ) * invln2 );
}
private static double invln2 = 1.0 / Math.log(2.0);
/**
* Calculate how many bits wide a number is,
* i.e. position of highest 1 bit.
* Fully unraveled binary search method.
* @return p where 2**p is first power of two >= n.
* e.g. binary 0001_0101 -> 5, 0xffffffff -> 32,
* 0 -> 0, 1 -> 1, 2 -> 2, 3 -> 2, 4 -> 3
* @author Dirk Bosmans Dirk.Bosmans@tijd.com */
public static final int widthInBits( int n )
{
if ( n < 0 ) return 32;
if ( n > 0x0000ffff )
{
if ( n > 0x00ffffff )
{
if ( n > 0x0fffffff )
{
if ( n > 0x3fffffff )
{
return 31;
}
else
{
if ( n > 0x1fffffff ) return 30;
else return 29;
}
}
else
{
if ( n > 0x03ffffff )
{
if ( n > 0x07ffffff ) return 28;
else return 27;
}
else
{
if ( n > 0x01ffffff ) return 26;
else return 25;
}
}
}
else
{
if ( n > 0x000fffff )
{
if ( n > 0x003fffff )
{
if ( n > 0x007fffff ) return 24;
else return 23;
}
else
{
if ( n > 0x001fffff ) return 22;
else return 21;
}
}
else
{
if ( n > 0x0003ffff )
{
if ( n > 0x0007ffff ) return 20;
else return 19;
}
else
{
if ( n > 0x0001ffff ) return 18;
else return 17;
}
}
}
}
else
{
if ( n > 0x000000ff )
{
if ( n > 0x00000fff )
{
if ( n > 0x00003fff )
{
if ( n > 0x00007fff ) return 16;
else return 15;
}
else
{
if ( n > 0x00001fff ) return 14;
else return 13;
}
}
else
{
if ( n > 0x000003ff )
{
if ( n > 0x000007ff ) return 12;
else return 11;
}
else
{
if ( n > 0x000001ff ) return 10;
else return 9;
}
}
}
else
{
if ( n > 0x0000000f )
{
if ( n > 0x0000003f )
{
if ( n > 0x0000007f ) return 8;
else return 7;
}
else
{
if ( n > 0x0000001f ) return 6;
else return 5;
}
}
else
{
if ( n > 0x00000003 )
{
if ( n > 0x00000007 ) return 4;
else return 3;
}
else
{
if ( n > 0x00000001 ) return 2;
return n;
}
}
}
}
}