Skip to content

Commit 59c6b21

Browse files
HardvanChiefpatwal
authored andcommitted
feat: Add countLeadingZeros new algorithm with Junit tests (TheAlgorithms#5703)
1 parent 40fc04b commit 59c6b21

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* bitmanipulation
2626
* [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java)
2727
* [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java)
28+
* [CountLeadingZeros](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java)
2829
* [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java)
2930
* [HighestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java)
3031
* [IndexOfRightMostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java)
@@ -649,6 +650,7 @@
649650
* bitmanipulation
650651
* [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java)
651652
* [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java)
653+
* [CountLeadingZerosTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java)
652654
* [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java)
653655
* [HighestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java)
654656
* [IndexOfRightMostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBitTest.java)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* CountLeadingZeros class contains a method to count the number of leading zeros in the binary representation of a number.
5+
* The number of leading zeros is the number of zeros before the leftmost 1 bit.
6+
* For example, the number 5 has 29 leading zeros in its 32-bit binary representation.
7+
* The number 0 has 32 leading zeros.
8+
* The number 1 has 31 leading zeros.
9+
* The number -1 has no leading zeros.
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class CountLeadingZeros {
14+
private CountLeadingZeros() {
15+
}
16+
17+
/**
18+
* Counts the number of leading zeros in the binary representation of a number.
19+
* Method: Keep shifting the mask to the right until the leftmost bit is 1.
20+
* The number of shifts is the number of leading zeros.
21+
*
22+
* @param num The input number.
23+
* @return The number of leading zeros.
24+
*/
25+
public static int countLeadingZeros(int num) {
26+
if (num == 0) {
27+
return 32;
28+
}
29+
30+
int count = 0;
31+
int mask = 1 << 31;
32+
while ((mask & num) == 0) {
33+
count++;
34+
mask >>>= 1;
35+
}
36+
37+
return count;
38+
}
39+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class CountLeadingZerosTest {
8+
9+
@Test
10+
public void testCountLeadingZeros() {
11+
assertEquals(29, CountLeadingZeros.countLeadingZeros(5)); // 000...0101 has 29 leading zeros
12+
assertEquals(32, CountLeadingZeros.countLeadingZeros(0)); // 000...0000 has 32 leading zeros
13+
assertEquals(31, CountLeadingZeros.countLeadingZeros(1)); // 000...0001 has 31 leading zeros
14+
assertEquals(0, CountLeadingZeros.countLeadingZeros(-1)); // No leading zeros in negative number (-1)
15+
}
16+
}

0 commit comments

Comments
 (0)