Repository: TheAlgorithms/Java
Pull request #4330 (2023-08-25 07:50:03)

added Highest Set Bit algo
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


BamaCharanChhandogi: If I do this 
// Handle negative numbers by converting to their absolute value
        if (num < 0) {
            num = Math.abs(num);
        }
BamaCharanChhandogi: It is a good idea but I think here -1 represents there are no index of highest set bit.
if you say to do this yeah I will do it.
vil02: > It is a good idea but I think here -1 represents there are no index of highest set bit. if you say to do this yeah I will do it.

Well you need to somehow _document_ that or _agree_ that the index -1 means that there is no such bit. In my opinion using `Optional` here requires less _documentation_.
@siriak what do you think?

@BamaCharanChhandogi I would still insist on [this suggestion](https://github.com/TheAlgorithms/Java/pull/4330#discussion_r1307922589).
BamaCharanChhandogi: hey @vil02! can you check?
siriak: @vil02 please review again and resolve comments.

P.S.
@vil02 could you add me in Discord or email me? I'd like to chat with you but cannot find any contact information
BamaCharanChhandogi: @siriak Check!

Patch:
diff --git a/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java
new file mode 100644
index 00000000..398b6bbb
--- /dev/null
+++ b/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java
@@ -0,0 +1,32 @@
+package com.thealgorithms.bitmanipulation;
+import java.util.Optional;
+
+/**
+ * Find Highest Set Bit
+ * This class provides a function calculating the position (or index)
+ * of the most significant bit being set to 1 in a given integer.
+ * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
+ */
+
+public final class HighestSetBit {
+    private HighestSetBit() {
+    }
+
+    public final static Optional<Integer> findHighestSetBit(int num) {
+        if (num < 0) {
+            throw new IllegalArgumentException("Input cannot be negative");
+        }
+
+        if (num == 0) {
+            return Optional.empty();
+        }
+
+        int position = 0;
+        while (num > 0) {
+            num >>= 1;
+            position++;
+        }
+
+        return Optional.of(position - 1);
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java b/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java
new file mode 100644
index 00000000..8dc61ae4
--- /dev/null
+++ b/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java
@@ -0,0 +1,37 @@
+package com.thealgorithms.bitmanipulation;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test case for Highest Set Bit
+ * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
+ */
+
+class HighestSetBitTest {
+
+    @Test
+    void testHighestSetBit() {
+        assertFalse(HighestSetBit.findHighestSetBit(0).isPresent());
+        assertEquals(0, HighestSetBit.findHighestSetBit(1).get());
+        assertEquals(1, HighestSetBit.findHighestSetBit(2).get());
+        assertEquals(1, HighestSetBit.findHighestSetBit(3).get());
+        assertEquals(2, HighestSetBit.findHighestSetBit(4).get());
+        assertEquals(2, HighestSetBit.findHighestSetBit(5).get());
+        assertEquals(2, HighestSetBit.findHighestSetBit(7).get());
+        assertEquals(3, HighestSetBit.findHighestSetBit(8).get());
+        assertEquals(3, HighestSetBit.findHighestSetBit(9).get());
+        assertEquals(3, HighestSetBit.findHighestSetBit(15).get());
+        assertEquals(4, HighestSetBit.findHighestSetBit(16).get());
+        assertEquals(4, HighestSetBit.findHighestSetBit(17).get());
+        assertEquals(4, HighestSetBit.findHighestSetBit(31).get());
+        assertEquals(5, HighestSetBit.findHighestSetBit(32).get());
+        assertEquals(5, HighestSetBit.findHighestSetBit(33).get());
+        assertEquals(7, HighestSetBit.findHighestSetBit(255).get());
+        assertEquals(8, HighestSetBit.findHighestSetBit(256).get());
+        assertEquals(8, HighestSetBit.findHighestSetBit(511).get());
+        assertEquals(9, HighestSetBit.findHighestSetBit(512).get());
+        assertThrows(IllegalArgumentException.class, () -> HighestSetBit.findHighestSetBit(-37));
+    }
+}