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

后端 未结 10 2156
情话喂你
情话喂你 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:08

    There is the function in guava libraries:

    LongMath.log2()
    

    So I suggest to use it.

提交回复
热议问题