Skip to content

Commit 230fd8d

Browse files
BAEL-4523 - Added code example for article to find the largest power of 2 that is less than the given number (eugenp#9840)
1 parent 3e2441b commit 230fd8d

2 files changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.algorithms.largestpowerof2;
2+
3+
import org.nd4j.linalg.io.Assert;
4+
5+
public class LargestPowerOf2 {
6+
public long findLargestPowerOf2LessThanTheGivenNumber(long input) {
7+
Assert.isTrue(input > 1, "Invalid input");
8+
9+
long firstPowerOf2 = 1;
10+
long nextPowerOf2 = 2;
11+
12+
while (nextPowerOf2 < input) {
13+
firstPowerOf2 = nextPowerOf2;
14+
nextPowerOf2 = nextPowerOf2 * 2;
15+
}
16+
return firstPowerOf2;
17+
}
18+
19+
public long findLargestPowerOf2LessThanTheGivenNumberUsingLogBase2(long input) {
20+
Assert.isTrue(input > 1, "Invalid input");
21+
22+
long temp = input;
23+
if (input % 2 == 0) {
24+
temp = input - 1;
25+
}
26+
27+
// Find log base 2 of a given number
28+
long power = (long) (Math.log(temp) / Math.log(2));
29+
long result = (long) Math.pow(2, power);
30+
31+
return result;
32+
}
33+
34+
public long findLargestPowerOf2LessThanTheGivenNumberUsingBitwiseAnd(long input) {
35+
Assert.isTrue(input > 1, "Invalid input");
36+
long result = 1;
37+
for (long i = input - 1; i > 1; i--) {
38+
if ((i & (i - 1)) == 0) {
39+
result = i;
40+
break;
41+
}
42+
}
43+
return result;
44+
}
45+
46+
public long findLargestPowerOf2LessThanTheGivenNumberUsingBitShiftApproach(long input) {
47+
Assert.isTrue(input > 1, "Invalid input");
48+
long result = 1;
49+
long powerOf2;
50+
51+
for (long i = 0; i < Long.BYTES * 8; i++) {
52+
powerOf2 = 1 << i;
53+
if (powerOf2 >= input) {
54+
break;
55+
}
56+
result = powerOf2;
57+
}
58+
return result;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.algorithms.largestpowerof2;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.junit.runners.Parameterized;
10+
11+
@RunWith(Parameterized.class)
12+
public class LargestPowerOf2UnitTest {
13+
private long input;
14+
private long expectedResult;
15+
16+
public LargestPowerOf2UnitTest(long input, long expectedResult) {
17+
this.input = input;
18+
this.expectedResult = expectedResult;
19+
}
20+
21+
@Parameterized.Parameters(name = "{index}: verifyLargestPowerOf2LessThanTheGivenNumber({0}) = {1}")
22+
public static Collection<Object[]> data() {
23+
return Arrays.asList(new Object[][] { { 2, 1 }, { 4, 2 }, { 500, 256 }, { 512, 256 }, { 1050, 1024 } });
24+
}
25+
26+
@Test
27+
public void givenValidInput_verifyLargestPowerOf2LessThanTheGivenNumber() {
28+
LargestPowerOf2 largestPowerOf2 = new LargestPowerOf2();
29+
30+
long result = largestPowerOf2.findLargestPowerOf2LessThanTheGivenNumber(input);
31+
32+
Assert.assertEquals(expectedResult, result);
33+
}
34+
35+
@Test
36+
public void givenValidInput_verifyLargestPowerOf2LessThanTheGivenNumberUsingLogBase2() {
37+
LargestPowerOf2 largestPowerOf2 = new LargestPowerOf2();
38+
39+
long result = largestPowerOf2.findLargestPowerOf2LessThanTheGivenNumberUsingLogBase2(input);
40+
41+
Assert.assertEquals(expectedResult, result);
42+
}
43+
44+
@Test
45+
public void givenValidInput_verifyLargestPowerOf2LessThanTheGivenNumberBitwiseAnd() {
46+
LargestPowerOf2 largestPowerOf2 = new LargestPowerOf2();
47+
48+
long result = largestPowerOf2.findLargestPowerOf2LessThanTheGivenNumberUsingBitwiseAnd(input);
49+
50+
Assert.assertEquals(expectedResult, result);
51+
}
52+
53+
@Test
54+
public void givenValidInput_verifyLargestPowerOf2LessThanTheGivenNumberBitShiftApproach() {
55+
LargestPowerOf2 largestPowerOf2 = new LargestPowerOf2();
56+
57+
long result = largestPowerOf2.findLargestPowerOf2LessThanTheGivenNumberUsingBitShiftApproach(input);
58+
59+
Assert.assertEquals(expectedResult, result);
60+
}
61+
62+
@Test(expected = IllegalArgumentException.class)
63+
public void givenInvalidInput_ShouldThrowException() {
64+
LargestPowerOf2 largestPowerOf2 = new LargestPowerOf2();
65+
largestPowerOf2.findLargestPowerOf2LessThanTheGivenNumber(1);
66+
}
67+
}

0 commit comments

Comments
 (0)