Created
June 24, 2015 00:58
-
-
Save xypaul/4701837c4360c1012781 to your computer and use it in GitHub Desktop.
Power of 2 - recursive and bitwise :)
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 powerCheck(int a) { | |
return a!=0 && (a & a-1) == 0; | |
} | |
public static Boolean recursivePowerCheck(int a){ | |
if (a == 1) return true; | |
if (a == 0 || a % 2 != 0) return false; | |
return recursivePowerCheck(a/2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment