Skip to content

Commit 357fc6a

Browse files
authored
Add LowestSetBit (#5567)
1 parent 7f57fc9 commit 357fc6a

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Lowest Set Bit
5+
* @author Prayas Kumar (https://github.com/prayas7102)
6+
*/
7+
8+
public final class LowestSetBit {
9+
// Private constructor to hide the default public one
10+
private LowestSetBit() {
11+
}
12+
/**
13+
* Isolates the lowest set bit of the given number. For example, if n = 18
14+
* (binary: 10010), the result will be 2 (binary: 00010).
15+
*
16+
* @param n the number whose lowest set bit will be isolated
17+
* @return the isolated lowest set bit of n
18+
*/
19+
public static int isolateLowestSetBit(int n) {
20+
// Isolate the lowest set bit using n & -n
21+
return n & -n;
22+
}
23+
/**
24+
* Clears the lowest set bit of the given number.
25+
* For example, if n = 18 (binary: 10010), the result will be 16 (binary: 10000).
26+
*
27+
* @param n the number whose lowest set bit will be cleared
28+
* @return the number after clearing its lowest set bit
29+
*/
30+
public static int clearLowestSetBit(int n) {
31+
// Clear the lowest set bit using n & (n - 1)
32+
return n & (n - 1);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Test case for Lowest Set Bit
9+
* @author Prayas Kumar (https://github.com/prayas7102)
10+
*/
11+
12+
public class LowestSetBitTest {
13+
14+
@Test
15+
void testLowestSetBitWithPositiveNumber() {
16+
// Test with a general positive number
17+
assertEquals(2, LowestSetBit.isolateLowestSetBit(18)); // 18 in binary: 10010, lowest bit is 2
18+
}
19+
20+
@Test
21+
void testLowestSetBitWithZero() {
22+
// Test with zero (edge case, no set bits)
23+
assertEquals(0, LowestSetBit.isolateLowestSetBit(0)); // 0 has no set bits, result should be 0
24+
}
25+
26+
@Test
27+
void testLowestSetBitWithOne() {
28+
// Test with number 1 (lowest set bit is 1 itself)
29+
assertEquals(1, LowestSetBit.isolateLowestSetBit(1)); // 1 in binary: 0001, lowest bit is 1
30+
}
31+
32+
@Test
33+
void testLowestSetBitWithPowerOfTwo() {
34+
// Test with a power of two (only one set bit)
35+
assertEquals(16, LowestSetBit.isolateLowestSetBit(16)); // 16 in binary: 10000, lowest bit is 16
36+
}
37+
38+
@Test
39+
void testLowestSetBitWithAllBitsSet() {
40+
// Test with a number with multiple set bits (like 7)
41+
assertEquals(1, LowestSetBit.isolateLowestSetBit(7)); // 7 in binary: 111, lowest bit is 1
42+
}
43+
44+
@Test
45+
void testLowestSetBitWithNegativeNumber() {
46+
// Test with a negative number (-1 in two's complement has all bits set)
47+
assertEquals(1, LowestSetBit.isolateLowestSetBit(-1)); // -1 in two's complement is all 1s, lowest bit is 1
48+
}
49+
50+
@Test
51+
void testLowestSetBitWithLargeNumber() {
52+
// Test with a large number
53+
assertEquals(64, LowestSetBit.isolateLowestSetBit(448)); // 448 in binary: 111000000, lowest bit is 64
54+
}
55+
56+
@Test
57+
void testClearLowestSetBitFor18() {
58+
// n = 18 (binary: 10010), expected result = 16 (binary: 10000)
59+
assertEquals(16, LowestSetBit.clearLowestSetBit(18));
60+
}
61+
62+
@Test
63+
void testClearLowestSetBitFor10() {
64+
// n = 10 (binary: 1010), expected result = 8 (binary: 1000)
65+
assertEquals(8, LowestSetBit.clearLowestSetBit(10));
66+
}
67+
68+
@Test
69+
void testClearLowestSetBitFor7() {
70+
// n = 7 (binary: 0111), expected result = 6 (binary: 0110)
71+
assertEquals(6, LowestSetBit.clearLowestSetBit(7));
72+
}
73+
74+
@Test
75+
void testClearLowestSetBitFor0() {
76+
// n = 0 (binary: 0000), no set bits to clear, expected result = 0
77+
assertEquals(0, LowestSetBit.clearLowestSetBit(0));
78+
}
79+
80+
@Test
81+
void testClearLowestSetBitForNegativeNumber() {
82+
// Test negative number to see how it behaves with two's complement
83+
// n = -1 (binary: all 1s in two's complement), expected result = -2 (clearing lowest set bit)
84+
assertEquals(-2, LowestSetBit.clearLowestSetBit(-1));
85+
}
86+
}

0 commit comments

Comments
 (0)