feat: Add countLeadingZeros new algorithm with Junit tests (#5703)

This commit is contained in:
Hardik Pawar
2024-10-12 11:49:55 +05:30
committed by GitHub
parent 966b4e369d
commit b8633ad14c
3 changed files with 57 additions and 0 deletions

View File

@ -25,6 +25,7 @@
* bitmanipulation
* [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java)
* [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java)
* [CountLeadingZeros](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java)
* [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java)
* [HighestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java)
* [IndexOfRightMostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java)
@ -649,6 +650,7 @@
* bitmanipulation
* [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java)
* [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java)
* [CountLeadingZerosTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java)
* [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java)
* [HighestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java)
* [IndexOfRightMostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBitTest.java)

View File

@ -0,0 +1,39 @@
package com.thealgorithms.bitmanipulation;
/**
* CountLeadingZeros class contains a method to count the number of leading zeros in the binary representation of a number.
* The number of leading zeros is the number of zeros before the leftmost 1 bit.
* For example, the number 5 has 29 leading zeros in its 32-bit binary representation.
* The number 0 has 32 leading zeros.
* The number 1 has 31 leading zeros.
* The number -1 has no leading zeros.
*
* @author Hardvan
*/
public final class CountLeadingZeros {
private CountLeadingZeros() {
}
/**
* Counts the number of leading zeros in the binary representation of a number.
* Method: Keep shifting the mask to the right until the leftmost bit is 1.
* The number of shifts is the number of leading zeros.
*
* @param num The input number.
* @return The number of leading zeros.
*/
public static int countLeadingZeros(int num) {
if (num == 0) {
return 32;
}
int count = 0;
int mask = 1 << 31;
while ((mask & num) == 0) {
count++;
mask >>>= 1;
}
return count;
}
}

View File

@ -0,0 +1,16 @@
package com.thealgorithms.bitmanipulation;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CountLeadingZerosTest {
@Test
public void testCountLeadingZeros() {
assertEquals(29, CountLeadingZeros.countLeadingZeros(5)); // 000...0101 has 29 leading zeros
assertEquals(32, CountLeadingZeros.countLeadingZeros(0)); // 000...0000 has 32 leading zeros
assertEquals(31, CountLeadingZeros.countLeadingZeros(1)); // 000...0001 has 31 leading zeros
assertEquals(0, CountLeadingZeros.countLeadingZeros(-1)); // No leading zeros in negative number (-1)
}
}