Skip to content

Commit 1617ed1

Browse files
Tuhinm2002alxkm
andauthored
feat : new bit manipulation algo FindNthBit of a number (#5731)
* feat : new algo uniquesubseqcount * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCount.java * feat : new bitmanipulation algo * feat : new bit algo * Update FindNthBitTest.java * Update FindNthBit.java --------- Co-authored-by: Alex Klymenko <[email protected]>
1 parent ac65af4 commit 1617ed1

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* A utility class to find the Nth bit of a given number.
5+
*
6+
* <p>This class provides a method to extract the value of the Nth bit (either 0 or 1)
7+
* from the binary representation of a given integer.
8+
*
9+
* <p>Example:
10+
* <pre>{@code
11+
* int result = FindNthBit.findNthBit(5, 2); // returns 0 as the 2nd bit of 5 (binary 101) is 0.
12+
* }</pre>
13+
*
14+
* <p>Author: <a href="https://github.com/Tuhinm2002">Tuhinm2002</a>
15+
*/
16+
public final class FindNthBit {
17+
18+
/**
19+
* Private constructor to prevent instantiation.
20+
*
21+
* <p>This is a utility class, and it should not be instantiated.
22+
* Attempting to instantiate this class will throw an UnsupportedOperationException.
23+
*/
24+
private FindNthBit() {
25+
throw new UnsupportedOperationException("Utility class");
26+
}
27+
28+
/**
29+
* Finds the value of the Nth bit of the given number.
30+
*
31+
* <p>This method uses bitwise operations to extract the Nth bit from the
32+
* binary representation of the given integer.
33+
*
34+
* @param num the integer number whose Nth bit is to be found
35+
* @param n the bit position (1-based) to retrieve
36+
* @return the value of the Nth bit (0 or 1)
37+
* @throws IllegalArgumentException if the bit position is less than 1
38+
*/
39+
public static int findNthBit(int num, int n) {
40+
if (n < 1) {
41+
throw new IllegalArgumentException("Bit position must be greater than or equal to 1.");
42+
}
43+
// Shifting the number to the right by (n - 1) positions and checking the last bit
44+
return (num & (1 << (n - 1))) >> (n - 1);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public final class FindNthBitTest {
11+
12+
/**
13+
* A parameterized test that checks the value of the Nth bit for different inputs.
14+
*
15+
* @param num the number whose Nth bit is being tested
16+
* @param n the bit position
17+
* @param expected the expected value of the Nth bit (0 or 1)
18+
*/
19+
@ParameterizedTest
20+
@MethodSource("provideTestCases")
21+
void findNthBitParameterizedTest(int num, int n, int expected) {
22+
assertEquals(expected, FindNthBit.findNthBit(num, n));
23+
}
24+
25+
/**
26+
* Provides the test cases as a stream of arguments for the parameterized test.
27+
*
28+
* @return a stream of test cases where each case consists of a number, the bit position,
29+
* and the expected result.
30+
*/
31+
private static Stream<Arguments> provideTestCases() {
32+
return Stream.of(Arguments.of(13, 2, 0), // binary: 1101, 2nd bit is 0
33+
Arguments.of(13, 3, 1), // binary: 1101, 3rd bit is 1
34+
Arguments.of(4, 2, 0), // binary: 100, 2nd bit is 0
35+
Arguments.of(4, 3, 1), // binary: 100, 3rd bit is 1
36+
Arguments.of(1, 1, 1) // binary: 1, 1st bit is 1
37+
);
38+
}
39+
}

0 commit comments

Comments
 (0)