From 5d0fb1e482db05ec3002ff099c7660d8cdfe7414 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 10 Oct 2024 20:21:19 +0530 Subject: [PATCH 1/6] feat: Add `HigherLowerPowerOfTwo` new algorithm with Junit tests --- .../HigherLowerPowerOfTwo.java | 50 +++++++++++++++++++ .../HigherLowerPowerOfTwoTest.java | 26 ++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java diff --git a/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java b/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java new file mode 100644 index 000000000000..84c6c5d2ab68 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java @@ -0,0 +1,50 @@ +package com.thealgorithms.bitmanipulation; + +/** + * HigherLowerPowerOfTwo class has two methods to find the next higher and lower power of two. + *

+ * nextHigherPowerOfTwo method finds the next higher power of two. + * nextLowerPowerOfTwo method finds the next lower power of two. + * Both methods take an integer as input and return the next higher or lower power of two. + * If the input is less than 1, the next higher power of two is 1. + * If the input is less than or equal to 1, the next lower power of two is 0. + * nextHigherPowerOfTwo method uses bitwise operations to find the next higher power of two. + * nextLowerPowerOfTwo method uses Integer.highestOneBit method to find the next lower power of two. + * The time complexity of both methods is O(1). + * The space complexity of both methods is O(1). + *

+ * + * @author Hardvan + */ +public final class HigherLowerPowerOfTwo { + private HigherLowerPowerOfTwo() { + } + + /** + * Finds the next higher power of two. + * + * @param x The given number. + * @return The next higher power of two. + */ + public static int nextHigherPowerOfTwo(int x) { + if (x < 1) return 1; + x--; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + return x + 1; + } + + /** + * Finds the next lower power of two. + * + * @param x The given number. + * @return The next lower power of two. + */ + public static int nextLowerPowerOfTwo(int x) { + if (x < 1) return 0; + return Integer.highestOneBit(x); + } +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java b/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java new file mode 100644 index 000000000000..ce78b1cf6e1d --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java @@ -0,0 +1,26 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class HigherLowerPowerOfTwoTest { + + @Test + public void testNextHigherPowerOfTwo() { + assertEquals(32, HigherLowerPowerOfTwo.nextHigherPowerOfTwo(19)); // next higher power of two is 32 + assertEquals(1, HigherLowerPowerOfTwo.nextHigherPowerOfTwo(1)); // next higher power of two is 1 + assertEquals(16, HigherLowerPowerOfTwo.nextHigherPowerOfTwo(15)); // next higher power of two is 16 + assertEquals(8, HigherLowerPowerOfTwo.nextHigherPowerOfTwo(8)); // next higher power of two is 8 + assertEquals(16, HigherLowerPowerOfTwo.nextHigherPowerOfTwo(9)); // next higher power of two is 16 + } + + @Test + public void testNextLowerPowerOfTwo() { + assertEquals(16, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(19)); // next lower power of two is 16 + assertEquals(1, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(1)); // next lower power of two is 1 + assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(9)); // next lower power of two is 8 + assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(15)); // next lower power of two is 8 + assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(8)); // next lower power of two is 8 + } +} From a8ce7f26c8416b9737089c4bea39fa7444238926 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Thu, 10 Oct 2024 14:51:39 +0000 Subject: [PATCH 2/6] Update directory --- DIRECTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index a1a53cb2926c..3514023bcea9 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -25,6 +25,7 @@ * bitmanipulation * [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java) * [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java) + * [HigherLowerPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java) * [HighestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java) * [IndexOfRightMostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java) * [IsEven](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsEven.java) @@ -36,6 +37,8 @@ * [OnesComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java) * [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java) * [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java) + * [SingleElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleElement.java) + * [SwapAdjacentBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java) * [TwosComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java) * ciphers * a5 @@ -638,6 +641,7 @@ * bitmanipulation * [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java) * [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java) + * [HigherLowerPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java) * [HighestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java) * [IndexOfRightMostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBitTest.java) * [IsEvenTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsEvenTest.java) @@ -649,6 +653,8 @@ * [OnesComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java) * [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java) * [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java) + * [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java) + * [SwapAdjacentBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java) * [TwosComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java) * ciphers * a5 From fdf2d35f1fd89726bee6ebad27dcda67ab3bb503 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 10 Oct 2024 20:24:24 +0530 Subject: [PATCH 3/6] Fix --- .../bitmanipulation/HigherLowerPowerOfTwoTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java b/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java index ce78b1cf6e1d..a3348201e425 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java @@ -18,9 +18,9 @@ public void testNextHigherPowerOfTwo() { @Test public void testNextLowerPowerOfTwo() { assertEquals(16, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(19)); // next lower power of two is 16 - assertEquals(1, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(1)); // next lower power of two is 1 - assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(9)); // next lower power of two is 8 - assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(15)); // next lower power of two is 8 - assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(8)); // next lower power of two is 8 + assertEquals(1, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(1)); // next lower power of two is 1 + assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(9)); // next lower power of two is 8 + assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(15));// next lower power of two is 8 + assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(8)); // next lower power of two is 8 } } From 06f9ecabaefebad02a0e02aece405a7a056a61bf Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 10 Oct 2024 20:25:40 +0530 Subject: [PATCH 4/6] Fix --- .../bitmanipulation/HigherLowerPowerOfTwoTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java b/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java index a3348201e425..34391002941b 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java @@ -20,7 +20,7 @@ public void testNextLowerPowerOfTwo() { assertEquals(16, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(19)); // next lower power of two is 16 assertEquals(1, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(1)); // next lower power of two is 1 assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(9)); // next lower power of two is 8 - assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(15));// next lower power of two is 8 + assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(15)); // next lower power of two is 8 assertEquals(8, HigherLowerPowerOfTwo.nextLowerPowerOfTwo(8)); // next lower power of two is 8 } } From 44308e7f665305adc805d87c346832a74f594c2f Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 10 Oct 2024 20:26:57 +0530 Subject: [PATCH 5/6] Fix --- .../bitmanipulation/HigherLowerPowerOfTwo.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java b/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java index 84c6c5d2ab68..0fb058b2b8a3 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java @@ -27,7 +27,9 @@ private HigherLowerPowerOfTwo() { * @return The next higher power of two. */ public static int nextHigherPowerOfTwo(int x) { - if (x < 1) return 1; + if (x < 1) { + return 1; + } x--; x |= x >> 1; x |= x >> 2; @@ -44,7 +46,9 @@ public static int nextHigherPowerOfTwo(int x) { * @return The next lower power of two. */ public static int nextLowerPowerOfTwo(int x) { - if (x < 1) return 0; + if (x < 1) { + return 0; + } return Integer.highestOneBit(x); } } From f0a292f2320435d066dd68c5a1d758e437658049 Mon Sep 17 00:00:00 2001 From: siriak Date: Sat, 12 Oct 2024 06:37:04 +0000 Subject: [PATCH 6/6] Update directory --- DIRECTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9d9849d36b51..880a5f12bdcc 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -28,6 +28,9 @@ * [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java) * [CountLeadingZeros](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java) * [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java) + * [GrayCodeConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/GrayCodeConversion.java) + * [HammingDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HammingDistance.java) + * [HigherLowerPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java) * [HighestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java) * [IndexOfRightMostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java) * [IsEven](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsEven.java) @@ -654,6 +657,9 @@ * [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java) * [CountLeadingZerosTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java) * [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java) + * [GrayCodeConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/GrayCodeConversionTest.java) + * [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HammingDistanceTest.java) + * [HigherLowerPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java) * [HighestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java) * [IndexOfRightMostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBitTest.java) * [IsEvenTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsEvenTest.java)