Skip to content

Commit 6346e7d

Browse files
add code for isPowerFour
1 parent f66da5e commit 6346e7d

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Utility class for checking if a number is a power of four.
5+
* A power of four is a number that can be expressed as 4^n where n is a non-negative integer.
6+
* This class provides a method to determine if a given integer is a power of four using bit manipulation.
7+
*/
8+
public final class IsPowerFour {
9+
private IsPowerFour() {
10+
}
11+
12+
/**
13+
* Checks if the given integer is a power of four.
14+
*
15+
* A number is considered a power of four if:
16+
* - It is greater than zero
17+
* - It is a power of two i.e. (n & (n - 1)) == 0
18+
* - Its single set bit is in an even position — verified by (n & 0xAAAAAAAA) == 0
19+
*
20+
* @param number the integer to check
21+
* @return true if the number is a power of false, false otherwise
22+
*/
23+
public static boolean IsPowerFour(int number) {
24+
if (number <= 0) {
25+
return false;
26+
}
27+
return (number & (number - 1)) == 0 && (number & 0xAAAAAAAA) == 0;
28+
}
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
/**
12+
* Test case for IsPowerFour class
13+
*/
14+
15+
public class IsPowerTwoTest {
16+
17+
@ParameterizedTest
18+
@MethodSource("provideNumbersForPowerFour")
19+
public void testIsPowerFour(int number, boolean expected) {
20+
if (expected) {
21+
assertTrue(IsPowerFour.isPowerFour(number));
22+
} else {
23+
assertFalse(IsPowerFour.isPowerFour(number));
24+
}
25+
}
26+
27+
private static Stream<Arguments> provideNumbersForPowerFour() {
28+
return Stream.of(
29+
Arguments.of(1, Boolean.TRUE), // 4^0
30+
Arguments.of(4, Boolean.TRUE), // 4^1
31+
Arguments.of(16, Boolean.TRUE), // 4^2
32+
Arguments.of(64, Boolean.TRUE), // 4^3
33+
Arguments.of(256, Boolean.TRUE), // 4^4
34+
Arguments.of(1024, Boolean.FALSE), // 1024 = 2^10, not 4^n
35+
Arguments.of(0, Boolean.FALSE), // 0 is not a power of four
36+
Arguments.of(-4, Boolean.FALSE), // Negative number
37+
Arguments.of(-16, Boolean.FALSE), // Negative number
38+
Arguments.of(2, Boolean.FALSE), // 2 is not a power of four
39+
Arguments.of(8, Boolean.FALSE), // 8 = 2^3, not 4^n
40+
Arguments.of(12, Boolean.FALSE), // 12 is not a power of four
41+
Arguments.of(20, Boolean.FALSE), // 20 is not a power of four
42+
Arguments.of(100, Boolean.FALSE), // 100 is not a power of four
43+
Arguments.of(4096, Boolean.TRUE) // 4^6 = 4096
44+
);
45+
}
46+
}

0 commit comments

Comments
 (0)