File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed
main/java/com/thealgorithms/bitmanipulation
test/java/com/thealgorithms/bitmanipulation Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 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 )
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 )
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments