How do you calculate log base 2 in Java for integers?

后端 未结 10 2139
情话喂你
情话喂你 2020-11-29 15:12

I use the following function to calculate log base 2 for integers:

public static int log2(int n){
    if(n <= 0) throw new IllegalArgumentException();
            


        
10条回答
  •  北海茫月
    2020-11-29 16:16

    Why not:

    public static double log2(int n)
    {
        return (Math.log(n) / Math.log(2));
    }
    

提交回复
热议问题