Hia, this is the fastest algorithm which will find the nearest, larger, 2^n number, given an integer. For example: 94 will yield 128. 265 will yield 512. etc.
I got it from here: http://mindprod.com/jgloss/widthinbits.html
The original author is Dirk Bosmans. His original algorithm returned the minimum number of bits required to store a given number. The only change I made is that the algorithm now returns 2^n instead of n.
The commented out algorithm is a slower, but cleaner version.
I will use this algorithm in my next post, the fastest texture-generating algorithm. but I thought this was worth mentioning by itself.
** edit: fixed bug in code. now the “branching method” gives equal results to the java.Math method.
/*public static double invln2 = 1.0 / Math.log(2.0);
public static int get2Fold(int num) {
return (int) Math.pow(2,Math.ceil(Math.log(num) * invln2));
}
*/
/**
* 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
*
* (Vincent) Changed to return 2^n instead of n
*
* @author Dirk Bosmans Dirk.Bosmans@tijd.com
* @author Vincent Vollers*/
static public final int get2Fold( int n )
{
if ( n < 0 ) return 32;
if ( n > 0x00010000 )
{
if ( n > 0x01000000 )
{
if ( n > 0x10000000 )
{
if ( n > 0x40000000 )
{
// if ( n > 0x7fffffff )
// return 32
// else
// return 2147483648; is too much, so return -1
return -1;
}
else
{
// !( n > 0x3fffffff )
if ( n > 0x20000000 ) return 1073741824;
else return 536870912;
}
}
else
{
// !( n > 0x0fffffff )
if ( n > 0x04000000 )
{
if ( n > 0x08000000 ) return 268435456;
else return 134217728;
}
else
{
// !( n > 0x03ffffff )
if ( n > 0x02000000 ) return 67108864;
else return 33554432;
}
}
}
else
{
// !( n > 0x00ffffff )
if ( n > 0x00100000 )
{
if ( n > 0x00400000 )
{
if ( n > 0x00800000 ) return 16777216;
else return 8388608;
}
else
{
// !( n > 0x003fffff )
if ( n > 0x00200000 ) return 4194304;
else return 2097152;
}
}
else
{
// !( n > 0x000fffff )
if ( n > 0x00040000 )
{
if ( n > 0x00080000 ) return 1048576;
else return 524288;
}
else
{
// !( n > 0x0003ffff )
if ( n > 0x00020000 ) return 262144;
else return 131072;
}
}
}
}
else
{
// !( n > 0x0000ffff )
if ( n > 0x00000100 )
{
if ( n > 0x00001000 )
{
if ( n > 0x00004000 )
{
if ( n > 0x00008000 ) return 65536;
else return 32768;
}
else
{
// !( n > 0x00003fff )
if ( n > 0x00002000 ) return 16384;
else return 8192;
}
}
else
{
// !( n > 0x00000fff )
if ( n > 0x00000400 )
{
if ( n > 0x00000800 ) return 4096;
else return 2048;
}
else
{
// !( n > 0x000003ff )
if ( n > 0x00000200 ) return 1024;
else return 512;
}
}
}
else
{
// !( n > 0x000000ff )
if ( n > 0x00000010 )
{
if ( n > 0x00000040 )
{
if ( n > 0x00000080 ) return 256;
else return 128;
}
else
{
// !( n > 0x0000003f )
if ( n > 0x00000020 ) return 64;
else return 32;
}
}
else
{
// !( n > 0x0000000f )
if ( n > 0x00000004 )
{
if ( n > 0x00000008 ) return 16;
else return 8;
}
else
{
// !( n > 0x00000003 )
if ( n > 0x00000002 ) return 4;
return n;
}
}
}
}
} // end widthInBits