Skip to content

Commit e4ef072

Browse files
authored
Add FirstDifferentBit algorithm (#5866)
1 parent 5d428d0 commit e4ef072

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* [CountLeadingZeros](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java)
3232
* [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java)
3333
* [FindNthBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/FindNthBit.java)
34+
* [FirstDifferentBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/FirstDifferentBit.java)
3435
* [GrayCodeConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/GrayCodeConversion.java)
3536
* [HammingDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HammingDistance.java)
3637
* [HigherLowerPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java)
@@ -727,6 +728,7 @@
727728
* [CountLeadingZerosTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java)
728729
* [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java)
729730
* [FindNthBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/FindNthBitTest.java)
731+
* [FirstDifferentBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/FirstDifferentBitTest.java)
730732
* [GrayCodeConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/GrayCodeConversionTest.java)
731733
* [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HammingDistanceTest.java)
732734
* [HigherLowerPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides a method to find the first differing bit
5+
* between two integers.
6+
*
7+
* Example:
8+
* x = 10 (1010 in binary)
9+
* y = 12 (1100 in binary)
10+
* The first differing bit is at index 1 (0-based)
11+
* So, the output will be 1
12+
*
13+
* @author Hardvan
14+
*/
15+
public final class FirstDifferentBit {
16+
private FirstDifferentBit() {
17+
}
18+
19+
/**
20+
* Identifies the index of the first differing bit between two integers.
21+
* Steps:
22+
* 1. XOR the two integers to get the differing bits
23+
* 2. Find the index of the first set bit in XOR result
24+
*
25+
* @param x the first integer
26+
* @param y the second integer
27+
* @return the index of the first differing bit (0-based)
28+
*/
29+
public static int firstDifferentBit(int x, int y) {
30+
int diff = x ^ y;
31+
return Integer.numberOfTrailingZeros(diff);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
8+
public class FirstDifferentBitTest {
9+
10+
@ParameterizedTest
11+
@CsvSource({"10, 8, 1", "7, 5, 1", "15, 14, 0", "1, 2, 0"})
12+
void testFirstDifferentBit(int x, int y, int expected) {
13+
assertEquals(expected, FirstDifferentBit.firstDifferentBit(x, y));
14+
}
15+
}

0 commit comments

Comments
 (0)