-
Notifications
You must be signed in to change notification settings - Fork 20k
added Highest Set Bit algo #4330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
siriak
merged 19 commits into
TheAlgorithms:master
from
BamaCharanChhandogi:HighestSetBit
Sep 5, 2023
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
923a015
added Highest Set Bit algo
BamaCharanChhandogi ef2801f
solved some issue
BamaCharanChhandogi b05992b
linter solved
BamaCharanChhandogi 1717f35
add negative number test case
BamaCharanChhandogi 73db001
linter solved
BamaCharanChhandogi 26126c6
linter solved2
BamaCharanChhandogi ac35530
linter solved3
BamaCharanChhandogi 095f22b
linter solved4
BamaCharanChhandogi 51e93f9
change code for efficient
BamaCharanChhandogi 8b5c7c7
linter solve
BamaCharanChhandogi ec8e63e
linter solve2
BamaCharanChhandogi 9155b7e
add get method
BamaCharanChhandogi 5319a52
added test cases
BamaCharanChhandogi 6f1995d
add remaining test cases
BamaCharanChhandogi 5d1ac83
last commit
BamaCharanChhandogi e9580a3
clang formater solved
BamaCharanChhandogi f5cc5f4
clang formater solved
BamaCharanChhandogi 3c234f3
documentation correct
BamaCharanChhandogi 7e13127
Merge branch 'master' into HighestSetBit
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
siriak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)); | ||
siriak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.