Created
February 11, 2011 22:50
-
-
Save robertmarsal/823214 to your computer and use it in GitHub Desktop.
recursive method to detect if a number is a power of 2
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
/** | |
* @author Robert Boloc | |
*/ | |
public boolean isPower2(int v) { | |
if (v == 1) { | |
return true; | |
} else if (v % 2 != 0 || v == 0) { | |
return false; | |
} else { | |
return isPower2(v / 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This also would work :)