mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 16:27:33 +08:00
Add HigherLowerPowerOfTwo
algorithm (#5707)
This commit is contained in:
@ -30,6 +30,7 @@
|
||||
* [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)
|
||||
@ -658,6 +659,7 @@
|
||||
* [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)
|
||||
|
@ -0,0 +1,54 @@
|
||||
package com.thealgorithms.bitmanipulation;
|
||||
|
||||
/**
|
||||
* HigherLowerPowerOfTwo class has two methods to find the next higher and lower power of two.
|
||||
* <p>
|
||||
* 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).
|
||||
* </p>
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user