Skip to content

Commit e15105b

Browse files
Add Power of Four Check using bit manipulation
- Implements isPowerOfFour method using bit manipulation - Checks if number is power of two and has bit at even position - Includes comprehensive unit tests - Fixes TheAlgorithms#6940
1 parent 2da0465 commit e15105b

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
* @author krishna-medapati (https://github.com/krishna-medapati)
9+
*/
10+
public final class PowerOfFour {
11+
private PowerOfFour() {
12+
}
13+
14+
/**
15+
* Checks if the given integer is a power of four.
16+
*
17+
* A number is considered a power of four if:
18+
* 1. It is greater than zero
19+
* 2. It has exactly one '1' bit in its binary representation (power of two)
20+
* 3. The '1' bit is at an even position (0, 2, 4, 6, ...)
21+
*
22+
* The method uses the mask 0x55555555 (binary: 01010101010101010101010101010101)
23+
* to check if the set bit is at an even position.
24+
*
25+
* @param number the integer to check
26+
* @return true if the number is a power of four, false otherwise
27+
*/
28+
public static boolean isPowerOfFour(int number) {
29+
if (number <= 0) {
30+
return false;
31+
}
32+
boolean isPowerOfTwo = (number & (number - 1)) == 0;
33+
boolean hasEvenBitPosition = (number & 0x55555555) != 0;
34+
return isPowerOfTwo && hasEvenBitPosition;
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
import org.junit.jupiter.api.Test;
6+
7+
class PowerOfFourTest {
8+
9+
@Test
10+
void testPowersOfFour() {
11+
assertTrue(PowerOfFour.isPowerOfFour(1));
12+
assertTrue(PowerOfFour.isPowerOfFour(4));
13+
assertTrue(PowerOfFour.isPowerOfFour(16));
14+
assertTrue(PowerOfFour.isPowerOfFour(64));
15+
assertTrue(PowerOfFour.isPowerOfFour(256));
16+
assertTrue(PowerOfFour.isPowerOfFour(1024));
17+
}
18+
19+
@Test
20+
void testNonPowersOfFour() {
21+
assertFalse(PowerOfFour.isPowerOfFour(2));
22+
assertFalse(PowerOfFour.isPowerOfFour(3));
23+
assertFalse(PowerOfFour.isPowerOfFour(5));
24+
assertFalse(PowerOfFour.isPowerOfFour(8));
25+
assertFalse(PowerOfFour.isPowerOfFour(15));
26+
assertFalse(PowerOfFour.isPowerOfFour(32));
27+
}
28+
29+
@Test
30+
void testEdgeCases() {
31+
assertFalse(PowerOfFour.isPowerOfFour(0));
32+
assertFalse(PowerOfFour.isPowerOfFour(-1));
33+
assertFalse(PowerOfFour.isPowerOfFour(-4));
34+
}
35+
}

0 commit comments

Comments
 (0)