MathUtil { public static long log2 (final long a) { if (a <= 0) throw new RuntimeException ("The argument must be strictly positive"); long v = a; final long b[] = new long[] { 0x00000002, 0x0000000c, 0x000000f0L, 0x0000ff00L, 0xffff0000L, 0xffffffff00000000L }; long r = 0; int s = 32; for (int i = 5; i >= 0; i--) { if ((v & b[i]) != 0) { v >>= s; r |= s; } s >>= 1; } return r; }  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 testLog2 () { assertEquals (0, MathUtil.log2 (1)); assertEquals (1, MathUtil.log2 (2)); assertEquals (1, MathUtil.log2 (3)); assertEquals (2, MathUtil.log2 (4)); assertEquals (2, MathUtil.log2 (6)); assertEquals (3, MathUtil.log2 (8)); assertEquals (4, MathUtil.log2 (16)); assertEquals (9, MathUtil.log2 (1023)); assertEquals (10, MathUtil.log2 (1024)); assertEquals (10, MathUtil.log2 (1025)); assertEquals (14, MathUtil.log2 (16384)); }