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 25
25
* bitmanipulation
26
26
* [ BitSwap] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java )
27
27
* [ 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 )
28
29
* [ CountSetBits] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java )
29
30
* [ HighestSetBit] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java )
30
31
* [ IndexOfRightMostSetBit] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java )
649
650
* bitmanipulation
650
651
* [ BitSwapTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java )
651
652
* [ 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 )
652
654
* [ CountSetBitsTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java )
653
655
* [ HighestSetBitTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java )
654
656
* [ 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