From e15105b8f71d62563227a6789c9dbbbbadc48369 Mon Sep 17 00:00:00 2001 From: krishna-medapati Date: Thu, 13 Nov 2025 01:57:18 +0530 Subject: [PATCH 1/7] 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 --- .../bitmanipulation/PowerOfFour.java | 36 +++++++++++++++++++ .../bitmanipulation/PowerOfFourTest.java | 35 ++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java diff --git a/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java new file mode 100644 index 000000000000..36a89da9ff01 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -0,0 +1,36 @@ +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. + * + * @author krishna-medapati (https://github.com/krishna-medapati) + */ +public final class PowerOfFour { + private PowerOfFour() { + } + + /** + * Checks if the given integer is a power of four. + * + * A number is considered a power of four if: + * 1. It is greater than zero + * 2. It has exactly one '1' bit in its binary representation (power of two) + * 3. The '1' bit is at an even position (0, 2, 4, 6, ...) + * + * The method uses the mask 0x55555555 (binary: 01010101010101010101010101010101) + * to check if the set bit is at an even position. + * + * @param number the integer to check + * @return true if the number is a power of four, false otherwise + */ + public static boolean isPowerOfFour(int number) { + if (number <= 0) { + return false; + } + boolean isPowerOfTwo = (number & (number - 1)) == 0; + boolean hasEvenBitPosition = (number & 0x55555555) != 0; + return isPowerOfTwo && hasEvenBitPosition; + } +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java new file mode 100644 index 000000000000..a6b0d7b03e5e --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java @@ -0,0 +1,35 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +class PowerOfFourTest { + + @Test + void testPowersOfFour() { + assertTrue(PowerOfFour.isPowerOfFour(1)); + assertTrue(PowerOfFour.isPowerOfFour(4)); + assertTrue(PowerOfFour.isPowerOfFour(16)); + assertTrue(PowerOfFour.isPowerOfFour(64)); + assertTrue(PowerOfFour.isPowerOfFour(256)); + assertTrue(PowerOfFour.isPowerOfFour(1024)); + } + + @Test + void testNonPowersOfFour() { + assertFalse(PowerOfFour.isPowerOfFour(2)); + assertFalse(PowerOfFour.isPowerOfFour(3)); + assertFalse(PowerOfFour.isPowerOfFour(5)); + assertFalse(PowerOfFour.isPowerOfFour(8)); + assertFalse(PowerOfFour.isPowerOfFour(15)); + assertFalse(PowerOfFour.isPowerOfFour(32)); + } + + @Test + void testEdgeCases() { + assertFalse(PowerOfFour.isPowerOfFour(0)); + assertFalse(PowerOfFour.isPowerOfFour(-1)); + assertFalse(PowerOfFour.isPowerOfFour(-4)); + } +} From 450402518edea3e7f2c67ed6c13952f1925f7ea2 Mon Sep 17 00:00:00 2001 From: krishna-medapati Date: Thu, 13 Nov 2025 01:59:20 +0530 Subject: [PATCH 2/7] Fix code formatting in PowerOfFourTest --- .../com/thealgorithms/bitmanipulation/PowerOfFourTest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java index a6b0d7b03e5e..c7dfcebe0447 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java @@ -2,10 +2,11 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; + import org.junit.jupiter.api.Test; class PowerOfFourTest { - + @Test void testPowersOfFour() { assertTrue(PowerOfFour.isPowerOfFour(1)); @@ -15,7 +16,7 @@ void testPowersOfFour() { assertTrue(PowerOfFour.isPowerOfFour(256)); assertTrue(PowerOfFour.isPowerOfFour(1024)); } - + @Test void testNonPowersOfFour() { assertFalse(PowerOfFour.isPowerOfFour(2)); @@ -25,7 +26,7 @@ void testNonPowersOfFour() { assertFalse(PowerOfFour.isPowerOfFour(15)); assertFalse(PowerOfFour.isPowerOfFour(32)); } - + @Test void testEdgeCases() { assertFalse(PowerOfFour.isPowerOfFour(0)); From b0264b8a4710c2394052f579e278b0e3cf0edfe5 Mon Sep 17 00:00:00 2001 From: krishna-medapati Date: Sat, 15 Nov 2025 15:05:11 +0530 Subject: [PATCH 3/7] Move PowerOfFour classes to maths package --- .../672a537f05f3798bd4f57d2d.md | 0 .../thealgorithms/{bitmanipulation => maths}/PowerOfFour.java | 2 +- .../{bitmanipulation => maths}/PowerOfFourTest.java | 0 3 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 curriculum/challenges/english/blocks/lecture-importance-of-accessibility-and-good-html-structure/672a537f05f3798bd4f57d2d.md rename src/main/java/com/thealgorithms/{bitmanipulation => maths}/PowerOfFour.java (96%) rename src/test/java/com/thealgorithms/{bitmanipulation => maths}/PowerOfFourTest.java (100%) diff --git a/curriculum/challenges/english/blocks/lecture-importance-of-accessibility-and-good-html-structure/672a537f05f3798bd4f57d2d.md b/curriculum/challenges/english/blocks/lecture-importance-of-accessibility-and-good-html-structure/672a537f05f3798bd4f57d2d.md new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/main/java/com/thealgorithms/maths/PowerOfFour.java similarity index 96% rename from src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java rename to src/main/java/com/thealgorithms/maths/PowerOfFour.java index 36a89da9ff01..e5fe6255821b 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java +++ b/src/main/java/com/thealgorithms/maths/PowerOfFour.java @@ -1,4 +1,4 @@ -package com.thealgorithms.bitmanipulation; +package com.thealgorithms.maths; /** * Utility class for checking if a number is a power of four. diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java b/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java similarity index 100% rename from src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java rename to src/test/java/com/thealgorithms/maths/PowerOfFourTest.java From dba445f2629aeba98f2582b9d005aef0c9e45067 Mon Sep 17 00:00:00 2001 From: krishna-medapati Date: Sat, 15 Nov 2025 15:11:58 +0530 Subject: [PATCH 4/7] Fix package declaration in PowerOfFourTest --- src/test/java/com/thealgorithms/maths/PowerOfFourTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java b/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java index c7dfcebe0447..1d762d13d272 100644 --- a/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java +++ b/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java @@ -1,4 +1,6 @@ -package com.thealgorithms.bitmanipulation; +package com.thealgorithms.maths; + +import com.thealgorithms.maths.PowerOfFour; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -34,3 +36,4 @@ void testEdgeCases() { assertFalse(PowerOfFour.isPowerOfFour(-4)); } } + From ae5f3a21cc28272177be22b30431d9638adf9147 Mon Sep 17 00:00:00 2001 From: krishna-medapati Date: Sat, 15 Nov 2025 15:13:26 +0530 Subject: [PATCH 5/7] Fix code formatting in PowerOfFourTest --- src/test/java/com/thealgorithms/maths/PowerOfFourTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java b/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java index 1d762d13d272..ca84ce3f675f 100644 --- a/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java +++ b/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java @@ -1,10 +1,9 @@ package com.thealgorithms.maths; -import com.thealgorithms.maths.PowerOfFour; - import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import com.thealgorithms.maths.PowerOfFour; import org.junit.jupiter.api.Test; class PowerOfFourTest { @@ -36,4 +35,3 @@ void testEdgeCases() { assertFalse(PowerOfFour.isPowerOfFour(-4)); } } - From 213d5c01a09ea3732b58f9a95e14f6b3a223f7ce Mon Sep 17 00:00:00 2001 From: krishna-medapati Date: Sat, 15 Nov 2025 15:21:17 +0530 Subject: [PATCH 6/7] Remove redundant import from PowerOfFourTest --- src/test/java/com/thealgorithms/maths/PowerOfFourTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java b/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java index ca84ce3f675f..c91f8b3cf1b5 100644 --- a/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java +++ b/src/test/java/com/thealgorithms/maths/PowerOfFourTest.java @@ -3,7 +3,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import com.thealgorithms.maths.PowerOfFour; import org.junit.jupiter.api.Test; class PowerOfFourTest { From f292e81ad1b164fa917894512bbe9f8ffb5250d9 Mon Sep 17 00:00:00 2001 From: krishna-medapati Date: Sat, 15 Nov 2025 20:05:30 +0530 Subject: [PATCH 7/7] Remove unrelated file --- .../672a537f05f3798bd4f57d2d.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 curriculum/challenges/english/blocks/lecture-importance-of-accessibility-and-good-html-structure/672a537f05f3798bd4f57d2d.md diff --git a/curriculum/challenges/english/blocks/lecture-importance-of-accessibility-and-good-html-structure/672a537f05f3798bd4f57d2d.md b/curriculum/challenges/english/blocks/lecture-importance-of-accessibility-and-good-html-structure/672a537f05f3798bd4f57d2d.md deleted file mode 100644 index e69de29bb2d1..000000000000