File tree 1 file changed +18
-4
lines changed
src/main/java/com/thealgorithms/bitmanipulation
1 file changed +18
-4
lines changed Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .bitmanipulation ;
2
2
3
3
/**
4
- * Find The Index Of Right Most SetBit
5
- * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
4
+ * Utility class for bit manipulation operations.
5
+ * This class provides methods to work with bitwise operations.
6
+ * Specifically, it includes a method to find the index of the rightmost set bit
7
+ * in an integer.
8
+ * This class is not meant to be instantiated.
9
+ *
10
+ * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
6
11
*/
7
-
8
12
public final class IndexOfRightMostSetBit {
13
+
9
14
private IndexOfRightMostSetBit () {
10
15
}
16
+
17
+ /**
18
+ * Finds the index of the rightmost set bit in the given integer.
19
+ * The index is zero-based, meaning the rightmost bit has an index of 0.
20
+ *
21
+ * @param n the integer to check for the rightmost set bit
22
+ * @return the index of the rightmost set bit; -1 if there are no set bits
23
+ * (i.e., the input integer is 0)
24
+ */
11
25
public static int indexOfRightMostSetBit (int n ) {
12
26
if (n == 0 ) {
13
27
return -1 ; // No set bits
@@ -16,7 +30,7 @@ public static int indexOfRightMostSetBit(int n) {
16
30
// Handle negative numbers by finding the two's complement
17
31
if (n < 0 ) {
18
32
n = -n ;
19
- n = n & (~n + 1 ); // Get the rightmost set bit in positive form
33
+ n = n & (~n + 1 ); // Isolate the rightmost set bit
20
34
}
21
35
22
36
int index = 0 ;
You can’t perform that action at this time.
0 commit comments