From 6346e7d537615dae64ef57be4205a43371162bd6 Mon Sep 17 00:00:00 2001 From: Vaishnavi Singh Date: Thu, 23 Oct 2025 21:21:37 +0530 Subject: [PATCH 1/6] add code for isPowerFour --- .../bitmanipulation/IsPowerFour.java | 29 ++++++++++++ .../bitmanipulation/IsPowerFourTest.java | 46 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java diff --git a/src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java b/src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java new file mode 100644 index 000000000000..6832d6f8ff99 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java @@ -0,0 +1,29 @@ +package com.thealgorithms.bitmanipulation; + +/** + * Utility class for checking if a number is a power of four. + * A power of four is a number that can be expressed as 4^n where n is a non-negative integer. + * This class provides a method to determine if a given integer is a power of four using bit manipulation. + */ +public final class IsPowerFour { + private IsPowerFour() { + } + + /** + * Checks if the given integer is a power of four. + * + * A number is considered a power of four if: + * - It is greater than zero + * - It is a power of two i.e. (n & (n - 1)) == 0 + * - Its single set bit is in an even position — verified by (n & 0xAAAAAAAA) == 0 + * + * @param number the integer to check + * @return true if the number is a power of false, false otherwise + */ + public static boolean IsPowerFour(int number) { + if (number <= 0) { + return false; + } + return (number & (number - 1)) == 0 && (number & 0xAAAAAAAA) == 0; + } +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java b/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java new file mode 100644 index 000000000000..99ad6d71d915 --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java @@ -0,0 +1,46 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * Test case for IsPowerFour class + */ + +public class IsPowerTwoTest { + + @ParameterizedTest + @MethodSource("provideNumbersForPowerFour") + public void testIsPowerFour(int number, boolean expected) { + if (expected) { + assertTrue(IsPowerFour.isPowerFour(number)); + } else { + assertFalse(IsPowerFour.isPowerFour(number)); + } + } + + private static Stream provideNumbersForPowerFour() { + return Stream.of( + Arguments.of(1, Boolean.TRUE), // 4^0 + Arguments.of(4, Boolean.TRUE), // 4^1 + Arguments.of(16, Boolean.TRUE), // 4^2 + Arguments.of(64, Boolean.TRUE), // 4^3 + Arguments.of(256, Boolean.TRUE), // 4^4 + Arguments.of(1024, Boolean.FALSE), // 1024 = 2^10, not 4^n + Arguments.of(0, Boolean.FALSE), // 0 is not a power of four + Arguments.of(-4, Boolean.FALSE), // Negative number + Arguments.of(-16, Boolean.FALSE), // Negative number + Arguments.of(2, Boolean.FALSE), // 2 is not a power of four + Arguments.of(8, Boolean.FALSE), // 8 = 2^3, not 4^n + Arguments.of(12, Boolean.FALSE), // 12 is not a power of four + Arguments.of(20, Boolean.FALSE), // 20 is not a power of four + Arguments.of(100, Boolean.FALSE), // 100 is not a power of four + Arguments.of(4096, Boolean.TRUE) // 4^6 = 4096 + ); + } +} From 7563b9eefdbe1f0f682fcf626cc6b974e0b7dc8b Mon Sep 17 00:00:00 2001 From: Vaishnavi Singh Date: Thu, 23 Oct 2025 21:36:35 +0530 Subject: [PATCH 2/6] clang-format --- .../bitmanipulation/IsPowerFour.java | 2 +- .../bitmanipulation/IsPowerFourTest.java | 32 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java b/src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java index 6832d6f8ff99..05a2c34f8242 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java @@ -11,7 +11,7 @@ private IsPowerFour() { /** * Checks if the given integer is a power of four. - * + *

* A number is considered a power of four if: * - It is greater than zero * - It is a power of two i.e. (n & (n - 1)) == 0 diff --git a/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java b/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java index 99ad6d71d915..632422a5b8a3 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.stream.Stream; + import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -25,22 +26,21 @@ public void testIsPowerFour(int number, boolean expected) { } private static Stream provideNumbersForPowerFour() { - return Stream.of( - Arguments.of(1, Boolean.TRUE), // 4^0 - Arguments.of(4, Boolean.TRUE), // 4^1 - Arguments.of(16, Boolean.TRUE), // 4^2 - Arguments.of(64, Boolean.TRUE), // 4^3 - Arguments.of(256, Boolean.TRUE), // 4^4 - Arguments.of(1024, Boolean.FALSE), // 1024 = 2^10, not 4^n - Arguments.of(0, Boolean.FALSE), // 0 is not a power of four - Arguments.of(-4, Boolean.FALSE), // Negative number - Arguments.of(-16, Boolean.FALSE), // Negative number - Arguments.of(2, Boolean.FALSE), // 2 is not a power of four - Arguments.of(8, Boolean.FALSE), // 8 = 2^3, not 4^n - Arguments.of(12, Boolean.FALSE), // 12 is not a power of four - Arguments.of(20, Boolean.FALSE), // 20 is not a power of four - Arguments.of(100, Boolean.FALSE), // 100 is not a power of four - Arguments.of(4096, Boolean.TRUE) // 4^6 = 4096 + return Stream.of(Arguments.of(1, Boolean.TRUE), // 4^0 + Arguments.of(4, Boolean.TRUE), // 4^1 + Arguments.of(16, Boolean.TRUE), // 4^2 + Arguments.of(64, Boolean.TRUE), // 4^3 + Arguments.of(256, Boolean.TRUE), // 4^4 + Arguments.of(1024, Boolean.FALSE), // 1024 = 2^10, not 4^n + Arguments.of(0, Boolean.FALSE), // 0 is not a power of four + Arguments.of(-4, Boolean.FALSE), // Negative number + Arguments.of(-16, Boolean.FALSE), // Negative number + Arguments.of(2, Boolean.FALSE), // 2 is not a power of four + Arguments.of(8, Boolean.FALSE), // 8 = 2^3, not 4^n + Arguments.of(12, Boolean.FALSE), // 12 is not a power of four + Arguments.of(20, Boolean.FALSE), // 20 is not a power of four + Arguments.of(100, Boolean.FALSE), // 100 is not a power of four + Arguments.of(4096, Boolean.TRUE) // 4^6 = 4096 ); } } From 9aaaf32be15f65d9aab757994c9590cfbcf2f220 Mon Sep 17 00:00:00 2001 From: Vaishnavi Singh Date: Thu, 23 Oct 2025 21:39:40 +0530 Subject: [PATCH 3/6] lint --- .../java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java b/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java index 632422a5b8a3..26cd0595d594 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java @@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.stream.Stream; - import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; From df4c029e37a577a3cb98413c1b3420e417b867ac Mon Sep 17 00:00:00 2001 From: Vaishnavi Singh Date: Thu, 23 Oct 2025 21:42:07 +0530 Subject: [PATCH 4/6] correcting class name --- .../java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java b/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java index 26cd0595d594..a231f4b6517c 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java @@ -12,7 +12,7 @@ * Test case for IsPowerFour class */ -public class IsPowerTwoTest { +public class IsPowerFourTest { @ParameterizedTest @MethodSource("provideNumbersForPowerFour") From c1cabb22973f1bebd6902f728d46dd9ce999e47e Mon Sep 17 00:00:00 2001 From: Vaishnavi Singh Date: Thu, 23 Oct 2025 21:45:14 +0530 Subject: [PATCH 5/6] correcting method name --- .../java/com/thealgorithms/bitmanipulation/IsPowerFour.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java b/src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java index 05a2c34f8242..54183673dc06 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/IsPowerFour.java @@ -20,7 +20,7 @@ private IsPowerFour() { * @param number the integer to check * @return true if the number is a power of false, false otherwise */ - public static boolean IsPowerFour(int number) { + public static boolean isPowerFour(int number) { if (number <= 0) { return false; } From e292d96c05e806c1d84db5946df29e23f2269cb6 Mon Sep 17 00:00:00 2001 From: Vaishnavi Singh Date: Thu, 23 Oct 2025 21:50:26 +0530 Subject: [PATCH 6/6] correcting test case --- .../java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java b/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java index a231f4b6517c..4278a7d85632 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/IsPowerFourTest.java @@ -30,7 +30,7 @@ private static Stream provideNumbersForPowerFour() { Arguments.of(16, Boolean.TRUE), // 4^2 Arguments.of(64, Boolean.TRUE), // 4^3 Arguments.of(256, Boolean.TRUE), // 4^4 - Arguments.of(1024, Boolean.FALSE), // 1024 = 2^10, not 4^n + Arguments.of(1024, Boolean.TRUE), // 4^5 Arguments.of(0, Boolean.FALSE), // 0 is not a power of four Arguments.of(-4, Boolean.FALSE), // Negative number Arguments.of(-16, Boolean.FALSE), // Negative number