MathUtils { public static int previousPowerOfTwo( double value ) { int result = 0; int power = 1; while ( power <= value && Math.abs( value - power ) > EPSILON ) { power = power << 1; result++; } return result; }  static int round( double v ); static boolean isZero( double v ); static int previousPowerOfTwo( double value ); static int nextPowerOfTwoValue( double value ); static final double EPSILON; static final double SQRT2; }
@Test public void testNearestPowerOfTwo() { assertEquals( 9, MathUtils.previousPowerOfTwo( 511 ) ); assertEquals( 9, MathUtils.previousPowerOfTwo( 512 ) ); assertEquals( 10, MathUtils.previousPowerOfTwo( 513 ) ); }