Repository: TheAlgorithms/Java
Pull request #5567 (2024-10-04 07:56:07)

LowestSetBit implementation and tests added
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5567?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `50.00000%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 52.97%. Comparing base [(`be8df21`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/be8df2188c716389de5150487c0862b80d88bd4e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`9d2cc53`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9d2cc533e873a83d7ab7e946cb670acf534751d9?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5567?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...om/thealgorithms/bitmanipulation/LowestSetBit.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5567?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fbitmanipulation%2FLowestSetBit.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9iaXRtYW5pcHVsYXRpb24vTG93ZXN0U2V0Qml0LmphdmE=) | 50.00% | [1 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5567?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5567      +/-   ##
============================================
+ Coverage     52.95%   52.97%   +0.01%     
- Complexity     3309     3312       +3     
============================================
  Files           529      530       +1     
  Lines         15343    15345       +2     
  Branches       2925     2925              
============================================
+ Hits           8125     8129       +4     
  Misses         6882     6882              
+ Partials        336      334       -2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5567?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

siriak: Please fix build and Clang PR checks
prayas7102: > Please fix build and Clang PR checks

Build and Clang checks have successfully passed. 

Patch:
diff --git a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java
new file mode 100644
index 00000000..127b6fa2
--- /dev/null
+++ b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java
@@ -0,0 +1,34 @@
+package com.thealgorithms.bitmanipulation;
+
+/**
+ * Lowest Set Bit
+ * @author Prayas Kumar (https://github.com/prayas7102)
+ */
+
+public final class LowestSetBit {
+    // Private constructor to hide the default public one
+    private LowestSetBit() {
+    }
+    /**
+     * Isolates the lowest set bit of the given number. For example, if n = 18
+     * (binary: 10010), the result will be 2 (binary: 00010).
+     *
+     * @param n the number whose lowest set bit will be isolated
+     * @return the isolated lowest set bit of n
+     */
+    public static int isolateLowestSetBit(int n) {
+        // Isolate the lowest set bit using n & -n
+        return n & -n;
+    }
+    /**
+     * Clears the lowest set bit of the given number.
+     * For example, if n = 18 (binary: 10010), the result will be 16 (binary: 10000).
+     *
+     * @param n the number whose lowest set bit will be cleared
+     * @return the number after clearing its lowest set bit
+     */
+    public static int clearLowestSetBit(int n) {
+        // Clear the lowest set bit using n & (n - 1)
+        return n & (n - 1);
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java
new file mode 100644
index 00000000..4c4d3364
--- /dev/null
+++ b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java
@@ -0,0 +1,86 @@
+package com.thealgorithms.bitmanipulation;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test case for Lowest Set Bit
+ * @author Prayas Kumar (https://github.com/prayas7102)
+ */
+
+public class LowestSetBitTest {
+
+    @Test
+    void testLowestSetBitWithPositiveNumber() {
+        // Test with a general positive number
+        assertEquals(2, LowestSetBit.isolateLowestSetBit(18)); // 18 in binary: 10010, lowest bit is 2
+    }
+
+    @Test
+    void testLowestSetBitWithZero() {
+        // Test with zero (edge case, no set bits)
+        assertEquals(0, LowestSetBit.isolateLowestSetBit(0)); // 0 has no set bits, result should be 0
+    }
+
+    @Test
+    void testLowestSetBitWithOne() {
+        // Test with number 1 (lowest set bit is 1 itself)
+        assertEquals(1, LowestSetBit.isolateLowestSetBit(1)); // 1 in binary: 0001, lowest bit is 1
+    }
+
+    @Test
+    void testLowestSetBitWithPowerOfTwo() {
+        // Test with a power of two (only one set bit)
+        assertEquals(16, LowestSetBit.isolateLowestSetBit(16)); // 16 in binary: 10000, lowest bit is 16
+    }
+
+    @Test
+    void testLowestSetBitWithAllBitsSet() {
+        // Test with a number with multiple set bits (like 7)
+        assertEquals(1, LowestSetBit.isolateLowestSetBit(7)); // 7 in binary: 111, lowest bit is 1
+    }
+
+    @Test
+    void testLowestSetBitWithNegativeNumber() {
+        // Test with a negative number (-1 in two's complement has all bits set)
+        assertEquals(1, LowestSetBit.isolateLowestSetBit(-1)); // -1 in two's complement is all 1s, lowest bit is 1
+    }
+
+    @Test
+    void testLowestSetBitWithLargeNumber() {
+        // Test with a large number
+        assertEquals(64, LowestSetBit.isolateLowestSetBit(448)); // 448 in binary: 111000000, lowest bit is 64
+    }
+
+    @Test
+    void testClearLowestSetBitFor18() {
+        // n = 18 (binary: 10010), expected result = 16 (binary: 10000)
+        assertEquals(16, LowestSetBit.clearLowestSetBit(18));
+    }
+
+    @Test
+    void testClearLowestSetBitFor10() {
+        // n = 10 (binary: 1010), expected result = 8 (binary: 1000)
+        assertEquals(8, LowestSetBit.clearLowestSetBit(10));
+    }
+
+    @Test
+    void testClearLowestSetBitFor7() {
+        // n = 7 (binary: 0111), expected result = 6 (binary: 0110)
+        assertEquals(6, LowestSetBit.clearLowestSetBit(7));
+    }
+
+    @Test
+    void testClearLowestSetBitFor0() {
+        // n = 0 (binary: 0000), no set bits to clear, expected result = 0
+        assertEquals(0, LowestSetBit.clearLowestSetBit(0));
+    }
+
+    @Test
+    void testClearLowestSetBitForNegativeNumber() {
+        // Test negative number to see how it behaves with two's complement
+        // n = -1 (binary: all 1s in two's complement), expected result = -2 (clearing lowest set bit)
+        assertEquals(-2, LowestSetBit.clearLowestSetBit(-1));
+    }
+}