mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-23 20:44:39 +08:00
feat: Add countLeadingZeros
new algorithm with Junit tests (#5703)
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user