How to calculate log base 2 of an integer in Java

1 Answer

0 votes
public class MyClass {
    public static int log2(int x) {
        return (int)(Math.log(x) / Math.log(2));
    }
    public static void main(String args[]) {
        int n = 100;

        System.out.println(log2(n));
    }
}




/*
run:

6

*/

 



answered Mar 26, 2021 by avibootz
edited Dec 5, 2021 by avibootz

Related questions

2 answers 333 views
2 answers 213 views
1 answer 201 views
1 answer 207 views
...