Repository: TheAlgorithms/Java
Pull request #5731 (2024-10-12 08:13:12)

feat : new bit manipulation algo `FindNthBit` of a number
<!--
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/5731?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 `20.00000%` with `4 lines` in your changes missing coverage. Please review.
> Project coverage is 63.85%. Comparing base [(`ac65af4`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/ac65af44c9365d42da42fb71c0b76115677fc673?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`f51b11b`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/f51b11b024c861f51c8672de802290f9321998d4?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/5731?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [.../com/thealgorithms/bitmanipulation/FindNthBit.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5731?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fbitmanipulation%2FFindNthBit.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9iaXRtYW5pcHVsYXRpb24vRmluZE50aEJpdC5qYXZh) | 20.00% | [3 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5731?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    #5731      +/-   ##
============================================
- Coverage     63.89%   63.85%   -0.04%     
  Complexity     4167     4167              
============================================
  Files           580      581       +1     
  Lines         16273    16278       +5     
  Branches       3140     3140              
============================================
- Hits          10398    10395       -3     
- Misses         5452     5457       +5     
- Partials        423      426       +3     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5731?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).


Patch:
diff --git a/src/main/java/com/thealgorithms/bitmanipulation/FindNthBit.java b/src/main/java/com/thealgorithms/bitmanipulation/FindNthBit.java
new file mode 100644
index 00000000..7a35fc3f
--- /dev/null
+++ b/src/main/java/com/thealgorithms/bitmanipulation/FindNthBit.java
@@ -0,0 +1,46 @@
+package com.thealgorithms.bitmanipulation;
+
+/**
+ * A utility class to find the Nth bit of a given number.
+ *
+ * <p>This class provides a method to extract the value of the Nth bit (either 0 or 1)
+ * from the binary representation of a given integer.
+ *
+ * <p>Example:
+ * <pre>{@code
+ * int result = FindNthBit.findNthBit(5, 2); // returns 0 as the 2nd bit of 5 (binary 101) is 0.
+ * }</pre>
+ *
+ * <p>Author: <a href="https://github.com/Tuhinm2002">Tuhinm2002</a>
+ */
+public final class FindNthBit {
+
+    /**
+     * Private constructor to prevent instantiation.
+     *
+     * <p>This is a utility class, and it should not be instantiated.
+     * Attempting to instantiate this class will throw an UnsupportedOperationException.
+     */
+    private FindNthBit() {
+        throw new UnsupportedOperationException("Utility class");
+    }
+
+    /**
+     * Finds the value of the Nth bit of the given number.
+     *
+     * <p>This method uses bitwise operations to extract the Nth bit from the
+     * binary representation of the given integer.
+     *
+     * @param num the integer number whose Nth bit is to be found
+     * @param n   the bit position (1-based) to retrieve
+     * @return    the value of the Nth bit (0 or 1)
+     * @throws IllegalArgumentException if the bit position is less than 1
+     */
+    public static int findNthBit(int num, int n) {
+        if (n < 1) {
+            throw new IllegalArgumentException("Bit position must be greater than or equal to 1.");
+        }
+        // Shifting the number to the right by (n - 1) positions and checking the last bit
+        return (num & (1 << (n - 1))) >> (n - 1);
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/bitmanipulation/FindNthBitTest.java b/src/test/java/com/thealgorithms/bitmanipulation/FindNthBitTest.java
new file mode 100644
index 00000000..978003d2
--- /dev/null
+++ b/src/test/java/com/thealgorithms/bitmanipulation/FindNthBitTest.java
@@ -0,0 +1,39 @@
+package com.thealgorithms.bitmanipulation;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.stream.Stream;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+public final class FindNthBitTest {
+
+    /**
+     * A parameterized test that checks the value of the Nth bit for different inputs.
+     *
+     * @param num     the number whose Nth bit is being tested
+     * @param n       the bit position
+     * @param expected the expected value of the Nth bit (0 or 1)
+     */
+    @ParameterizedTest
+    @MethodSource("provideTestCases")
+    void findNthBitParameterizedTest(int num, int n, int expected) {
+        assertEquals(expected, FindNthBit.findNthBit(num, n));
+    }
+
+    /**
+     * Provides the test cases as a stream of arguments for the parameterized test.
+     *
+     * @return a stream of test cases where each case consists of a number, the bit position,
+     * and the expected result.
+     */
+    private static Stream<Arguments> provideTestCases() {
+        return Stream.of(Arguments.of(13, 2, 0), // binary: 1101, 2nd bit is 0
+            Arguments.of(13, 3, 1), // binary: 1101, 3rd bit is 1
+            Arguments.of(4, 2, 0), // binary: 100, 2nd bit is 0
+            Arguments.of(4, 3, 1), // binary: 100, 3rd bit is 1
+            Arguments.of(1, 1, 1) // binary: 1, 1st bit is 1
+        );
+    }
+}