Skip to content

feat : new bit manipulation algo FindNthBit of a number #5731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c7c3d8b
feat : new algo uniquesubseqcount
Tuhinm2002 Oct 5, 2024
d124680
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
d99c476
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 5, 2024
33f06a8
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
892e365
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
7858443
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
569cc1c
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
96a5566
Merge branch 'master' into master
Tuhinm2002 Oct 6, 2024
3b8d047
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
735077d
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
8819fdb
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
51db0c6
Merge branch 'master' into master
Tuhinm2002 Oct 7, 2024
6533338
Merge branch 'master' into master
Tuhinm2002 Oct 7, 2024
ed36d8a
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
13a3e7e
Merge branch 'master' into master
Tuhinm2002 Oct 7, 2024
697c108
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
002d55f
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
1536eeb
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
5288a5a
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
827b4d2
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
c7bd78a
Merge branch 'master' into master
Tuhinm2002 Oct 8, 2024
574fcf4
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 8, 2024
a505357
Merge branch 'master' into master
alxkm Oct 8, 2024
963201d
Merge branch 'TheAlgorithms:master' into master
Tuhinm2002 Oct 9, 2024
17f852d
Merge branch 'TheAlgorithms:master' into master
Tuhinm2002 Oct 9, 2024
2553434
feat : new bitmanipulation algo
Tuhinm2002 Oct 9, 2024
f3501d8
Merge branch 'TheAlgorithms:master' into master
Tuhinm2002 Oct 9, 2024
23be56a
Merge branch 'TheAlgorithms:master' into master
Tuhinm2002 Oct 12, 2024
f51b11b
feat : new bit algo
Tuhinm2002 Oct 12, 2024
4242f28
Update FindNthBitTest.java
Tuhinm2002 Oct 12, 2024
05280ab
Update FindNthBit.java
Tuhinm2002 Oct 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/main/java/com/thealgorithms/bitmanipulation/FindNthBit.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
);
}
}