Skip to content

Commit 3979e82

Browse files
Add Power of Four Check using bit manipulation (#7065)
* 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 #6940 * Fix code formatting in PowerOfFourTest * Move PowerOfFour classes to maths package * Fix package declaration in PowerOfFourTest * Fix code formatting in PowerOfFourTest * Remove redundant import from PowerOfFourTest * Remove unrelated file
1 parent 98eecb9 commit 3979e82

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-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.maths;
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class PowerOfFourTest {
9+
10+
@Test
11+
void testPowersOfFour() {
12+
assertTrue(PowerOfFour.isPowerOfFour(1));
13+
assertTrue(PowerOfFour.isPowerOfFour(4));
14+
assertTrue(PowerOfFour.isPowerOfFour(16));
15+
assertTrue(PowerOfFour.isPowerOfFour(64));
16+
assertTrue(PowerOfFour.isPowerOfFour(256));
17+
assertTrue(PowerOfFour.isPowerOfFour(1024));
18+
}
19+
20+
@Test
21+
void testNonPowersOfFour() {
22+
assertFalse(PowerOfFour.isPowerOfFour(2));
23+
assertFalse(PowerOfFour.isPowerOfFour(3));
24+
assertFalse(PowerOfFour.isPowerOfFour(5));
25+
assertFalse(PowerOfFour.isPowerOfFour(8));
26+
assertFalse(PowerOfFour.isPowerOfFour(15));
27+
assertFalse(PowerOfFour.isPowerOfFour(32));
28+
}
29+
30+
@Test
31+
void testEdgeCases() {
32+
assertFalse(PowerOfFour.isPowerOfFour(0));
33+
assertFalse(PowerOfFour.isPowerOfFour(-1));
34+
assertFalse(PowerOfFour.isPowerOfFour(-4));
35+
}
36+
}

0 commit comments

Comments
 (0)