Created
March 25, 2014 12:54
-
-
Save Experiment5X/9761259 to your computer and use it in GitHub Desktop.
I saw an interview question on reddit to check if a given number is a power of 2 in one line. Here's a solution in Java.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static boolean isPowerOf2(int num) | |
{ | |
return Math.log(num) / Math.log(2) == (double)(31 - Integer.numberOfLeadingZeros(num)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pussy.