// In Math this holds: loga(b) = ln(b) / ln(a) // loga is log with base a public static int log2(int N) { // calculate log2 N indirectly // using log() method int result = (int)(Math.log(N) / Math.log(2)); return result; }