code: public static int log2 (int n){
      int log = 0;
  
      for(int k=1; k < n; k *= 2, log++);
  
      if (n != (1 << log))
        return -1 ; /* n is not a power of 2 */
      return log; }
docs: Return the integer log, base 2, of n, or -1 if n is not an integral power of 2.