MathUtil { public static long getPrevPower2 (final long a) { long v = a; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v |= v >> 32; return (v >> 1) + 1; }  static int getGCD(int a, int b); static int getGCD(int... rgNumbers); static int getLCM(int a, int b); static int getLCM(int... rgNumbers); static boolean isPowerOfTwo(int nNum); static int sgn(final int a); static long log2(final long a); static long getPrevPower2(final long a); static int divCeil(int nNumerator, int nDenominator); static int max(int... a); static int min(int... a);  }
@Test public void testGetPrevPower2 () { for (int i = 1; i < 100; i++) System.out.println (i + " => " + MathUtil.getPrevPower2 (i)); assertEquals (1, MathUtil.getPrevPower2 (1)); assertEquals (2, MathUtil.getPrevPower2 (2)); assertEquals (2, MathUtil.getPrevPower2 (3)); assertEquals (4, MathUtil.getPrevPower2 (4)); assertEquals (4, MathUtil.getPrevPower2 (5)); assertEquals (4, MathUtil.getPrevPower2 (7)); assertEquals (8, MathUtil.getPrevPower2 (8)); assertEquals (8, MathUtil.getPrevPower2 (9)); assertEquals (64, MathUtil.getPrevPower2 (100)); assertEquals (128, MathUtil.getPrevPower2 (200)); assertEquals (128, MathUtil.getPrevPower2 (200)); assertEquals (1024, MathUtil.getPrevPower2 (1025)); assertEquals (1024, MathUtil.getPrevPower2 (2025)); assertEquals (2048, MathUtil.getPrevPower2 (2125)); assertEquals (4096, MathUtil.getPrevPower2 (4444)); assertEquals (65536, MathUtil.getPrevPower2 (100000)); assertEquals (524288, MathUtil.getPrevPower2 (1000000)); assertEquals (8388608, MathUtil.getPrevPower2 (10000000)); assertEquals (67108864, MathUtil.getPrevPower2 (100000000)); assertEquals (536870912, MathUtil.getPrevPower2 (1000000000)); }