I use the following function to calculate log base 2 for integers:
public static int log2(int n){ if(n <= 0) throw new IllegalArgumentException();
Try Math.log(x) / Math.log(2)
Math.log(x) / Math.log(2)