Skip to content

Instantly share code, notes, and snippets.

@booknara
Created April 21, 2016 22:24
Show Gist options
  • Save booknara/9a6aaef53df24e919de593f8125a25a7 to your computer and use it in GitHub Desktop.
Save booknara/9a6aaef53df24e919de593f8125a25a7 to your computer and use it in GitHub Desktop.
Checking the number of power of 4
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