From db1f33ea5f799f5737bac1498d9c0cef124e05b8 Mon Sep 17 00:00:00 2001 From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com> Date: Wed, 29 Oct 2025 18:11:32 +0530 Subject: [PATCH 1/5] Added PowerOfFour algorithm in bitmanipulation --- .../bitmanipulation/PowerOfFour.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java new file mode 100644 index 000000000000..dfde4aa2646f --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -0,0 +1,16 @@ +package bitmanipulation; + +public class PowerOfFour { + public static boolean isPowerOfFour(int n) { + // A power of 4 has only one bit set and that bit is at an even position + return n > 0 && (n & (n - 1)) == 0 && (n & 0x55555555) != 0; + } + + public static void main(String[] args) { + int num = 64; // change to test other numbers + if (isPowerOfFour(num)) + System.out.println(num + " is a power of 4."); + else + System.out.println(num + " is NOT a power of 4."); + } +} From 7dfe84e988544db24bef280aa53452c29d3ea25c Mon Sep 17 00:00:00 2001 From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com> Date: Wed, 29 Oct 2025 18:18:51 +0530 Subject: [PATCH 2/5] Moved PowerOfFour.java to main/java --- .../java/com/thealgorithms/bitmanipulation/PowerOfFour.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{test => main}/java/com/thealgorithms/bitmanipulation/PowerOfFour.java (100%) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java similarity index 100% rename from src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java rename to src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java From 08890bcc8e7ebc030ca0f82c459291516068ec3b Mon Sep 17 00:00:00 2001 From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com> Date: Wed, 29 Oct 2025 18:23:53 +0530 Subject: [PATCH 3/5] Fixed Checkstyle issues in PowerOfFour.java --- .../bitmanipulation/PowerOfFour.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java new file mode 100644 index 000000000000..a256b0816f2a --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -0,0 +1,16 @@ +package com.thealgorithms.bitmanipulation; + +public class PowerOfFour { + + private PowerOfFour() { + throw new IllegalStateException("Utility class"); + } + + public static boolean isPowerOfFour(int n) { + if (n <= 0) { + return false; + } else { + return (n & (n - 1)) == 0 && (n & 0x55555555) != 0; + } + } +} From 99f371b6eee339d65e87e88d7806d86d5f25211c Mon Sep 17 00:00:00 2001 From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com> Date: Wed, 29 Oct 2025 18:29:55 +0530 Subject: [PATCH 4/5] Fixed class name conflict and Checkstyle errors --- .../bitmanipulation/PowerOfFour.java | 32 +++++++++++------- .../bitmanipulation/PowerOfFour.java | 14 ++++++-- .../bitmanipulation/PowerOfFourTest.java | 33 +++++++++++++++++++ 3 files changed, 66 insertions(+), 13 deletions(-) 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 index dfde4aa2646f..fde287f5415c 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -1,16 +1,26 @@ -package bitmanipulation; +package com.thealgorithms.bitmanipulation; -public class PowerOfFour { - public static boolean isPowerOfFour(int n) { - // A power of 4 has only one bit set and that bit is at an even position - return n > 0 && (n & (n - 1)) == 0 && (n & 0x55555555) != 0; +/** + * This class provides a method to check if a given number is a power of four. + */ +public final class PowerOfFour { + + // Private constructor to prevent instantiation + private PowerOfFour() { + throw new AssertionError("Cannot instantiate utility class"); } - public static void main(String[] args) { - int num = 64; // change to test other numbers - if (isPowerOfFour(num)) - System.out.println(num + " is a power of 4."); - else - System.out.println(num + " is NOT a power of 4."); + /** + * Checks whether the given integer is a power of four. + * + * @param n the number to check + * @return true if n is a power of four, false otherwise + */ + public static boolean isPowerOfFour(int n) { + if (n <= 0) { + return false; + } else { + return (n & (n - 1)) == 0 && (n & 0x55555555) != 0; + } } } diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java index a256b0816f2a..fde287f5415c 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -1,11 +1,21 @@ package com.thealgorithms.bitmanipulation; -public class PowerOfFour { +/** + * This class provides a method to check if a given number is a power of four. + */ +public final class PowerOfFour { + // Private constructor to prevent instantiation private PowerOfFour() { - throw new IllegalStateException("Utility class"); + throw new AssertionError("Cannot instantiate utility class"); } + /** + * Checks whether the given integer is a power of four. + * + * @param n the number to check + * @return true if n is a power of four, false otherwise + */ public static boolean isPowerOfFour(int n) { if (n <= 0) { return false; 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..1d46f1202c8d --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java @@ -0,0 +1,33 @@ +package com.thealgorithms.bitmanipulation; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for PowerOfFour class. + */ +public final class PowerOfFourTest { + + @Test + void testPowerOfFourTrueCases() { + Assertions.assertTrue(PowerOfFour.isPowerOfFour(1)); + Assertions.assertTrue(PowerOfFour.isPowerOfFour(4)); + Assertions.assertTrue(PowerOfFour.isPowerOfFour(16)); + Assertions.assertTrue(PowerOfFour.isPowerOfFour(64)); + } + + @Test + void testPowerOfFourFalseCases() { + Assertions.assertFalse(PowerOfFour.isPowerOfFour(0)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(2)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(8)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(12)); + } + + @Test + void testNegativeNumbers() { + Assertions.assertFalse(PowerOfFour.isPowerOfFour(-4)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(-16)); + } +} + From 07022c40f958ab40436d38d797d593ba96083b59 Mon Sep 17 00:00:00 2001 From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com> Date: Wed, 29 Oct 2025 18:33:13 +0530 Subject: [PATCH 5/5] Fix Checkstyle and rename test class to PowerOfFourTest --- .../java/com/thealgorithms/bitmanipulation/PowerOfFour.java | 4 ++-- .../com/thealgorithms/bitmanipulation/PowerOfFourTest.java | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java index fde287f5415c..f91f8412b3ce 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -5,9 +5,9 @@ */ public final class PowerOfFour { - // Private constructor to prevent instantiation + /** Private constructor to prevent instantiation. */ private PowerOfFour() { - throw new AssertionError("Cannot instantiate utility class"); + throw new AssertionError("Cannot instantiate utility class."); } /** diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java index 1d46f1202c8d..b202a58ed591 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java @@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test; /** - * Unit tests for PowerOfFour class. + * Unit tests for {@link PowerOfFour}. */ public final class PowerOfFourTest { @@ -30,4 +30,3 @@ void testNegativeNumbers() { Assertions.assertFalse(PowerOfFour.isPowerOfFour(-16)); } } -