Skip to content

Commit 7f57fc9

Browse files
authored
Improve comments, function & class documentation in IndexOfRightMostSetBit.java (#5579)
1 parent dea806e commit 7f57fc9

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
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)
611
*/
7-
812
public final class IndexOfRightMostSetBit {
13+
914
private IndexOfRightMostSetBit() {
1015
}
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+
*/
1125
public static int indexOfRightMostSetBit(int n) {
1226
if (n == 0) {
1327
return -1; // No set bits
@@ -16,7 +30,7 @@ public static int indexOfRightMostSetBit(int n) {
1630
// Handle negative numbers by finding the two's complement
1731
if (n < 0) {
1832
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
2034
}
2135

2236
int index = 0;

0 commit comments

Comments
 (0)