From c736ee0787d2a624e6399a526715e3f2e3c399f5 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sat, 5 Oct 2024 11:11:41 +0530 Subject: [PATCH 1/2] Improve comments, function & class documentation in `IndexOfRightMostSetBit.java` --- .../IndexOfRightMostSetBit.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java index b825916a8674..ac8d3d31f56b 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java @@ -1,13 +1,27 @@ package com.thealgorithms.bitmanipulation; /** - * Find The Index Of Right Most SetBit - * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) + * Utility class for bit manipulation operations. + * This class provides methods to work with bitwise operations. + * Specifically, it includes a method to find the index of the rightmost set bit + * in an integer. + * This class is not meant to be instantiated. + * + * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) */ - public final class IndexOfRightMostSetBit { + private IndexOfRightMostSetBit() { } + + /** + * Finds the index of the rightmost set bit in the given integer. + * The index is zero-based, meaning the rightmost bit has an index of 0. + * + * @param n the integer to check for the rightmost set bit + * @return the index of the rightmost set bit; -1 if there are no set bits + * (i.e., the input integer is 0) + */ public static int indexOfRightMostSetBit(int n) { if (n == 0) { return -1; // No set bits @@ -16,7 +30,7 @@ public static int indexOfRightMostSetBit(int n) { // Handle negative numbers by finding the two's complement if (n < 0) { n = -n; - n = n & (~n + 1); // Get the rightmost set bit in positive form + n = n & (~n + 1);// Isolate the rightmost set bit } int index = 0; From 86d1f76ea1d15fb4d614eacdf3d4b72f1f3ac5fa Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sat, 5 Oct 2024 11:12:46 +0530 Subject: [PATCH 2/2] Fix comment --- .../thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java index ac8d3d31f56b..1b8962344ea7 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java @@ -30,7 +30,7 @@ public static int indexOfRightMostSetBit(int n) { // Handle negative numbers by finding the two's complement if (n < 0) { n = -n; - n = n & (~n + 1);// Isolate the rightmost set bit + n = n & (~n + 1); // Isolate the rightmost set bit } int index = 0;