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
* [ CountLeadingZeros] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java )
28
+ * [ ClearLeftmostSetBit] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.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
* [ CountLeadingZerosTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java )
653
+ * [ ClearLeftmostSetBitTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.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
+ * ClearLeftmostSetBit class contains a method to clear the leftmost set bit of a number.
5
+ * The leftmost set bit is the leftmost bit that is set to 1 in the binary representation of a number.
6
+ *
7
+ * Example:
8
+ * 26 (11010) -> 10 (01010)
9
+ * 1 (1) -> 0 (0)
10
+ * 7 (111) -> 3 (011)
11
+ * 6 (0110) -> 2 (0010)
12
+ *
13
+ * @author Hardvan
14
+ */
15
+ public final class ClearLeftmostSetBit {
16
+ private ClearLeftmostSetBit () {
17
+ }
18
+
19
+ /**
20
+ * Clears the leftmost set bit (1) of a given number.
21
+ * Step 1: Find the position of the leftmost set bit
22
+ * Step 2: Create a mask with all bits set except for the leftmost set bit
23
+ * Step 3: Clear the leftmost set bit using AND with the mask
24
+ *
25
+ * @param num The input number.
26
+ * @return The number after clearing the leftmost set bit.
27
+ */
28
+ public static int clearLeftmostSetBit (int num ) {
29
+ int pos = 0 ;
30
+ int temp = num ;
31
+ while (temp > 0 ) {
32
+ temp >>= 1 ;
33
+ pos ++;
34
+ }
35
+
36
+ int mask = ~(1 << (pos - 1 ));
37
+ return num & mask ;
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 ClearLeftmostSetBitTest {
8
+
9
+ @ Test
10
+ public void testClearLeftmostSetBit () {
11
+ assertEquals (10 , ClearLeftmostSetBit .clearLeftmostSetBit (26 )); // 11010 -> 01010
12
+ assertEquals (0 , ClearLeftmostSetBit .clearLeftmostSetBit (1 )); // 1 -> 0
13
+ assertEquals (3 , ClearLeftmostSetBit .clearLeftmostSetBit (7 )); // 111 -> 011
14
+ assertEquals (2 , ClearLeftmostSetBit .clearLeftmostSetBit (6 )); // 0110 -> 0010
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments