Created
April 21, 2016 22:24
-
-
Save booknara/9a6aaef53df24e919de593f8125a25a7 to your computer and use it in GitHub Desktop.
Checking the number of power of 4
This file contains 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 class Power4 { | |
public static void main(String[] args) { | |
for (int i = 1; i < 30; i++) { | |
if (isPower4(i)) | |
System.out.println(i + " is power of 4"); | |
else | |
System.out.println(i + " is NOT power of 4"); | |
} | |
} | |
public static boolean isPower4(int value) { | |
return isEnhancedPower2(value) && ((value - 1) % 3 == 0); | |
} | |
public static boolean isEnhancedPower2(int value) { | |
return ((value & -value) == value); | |
} | |
public static boolean isEnhanced2Power2(int value) { | |
return ((value & value - 1) == 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment