-
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
added Highest Set Bit algo #4330
Conversation
src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java
Outdated
Show resolved
Hide resolved
If I do this OR(No negative number allow) if(num<=0){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To handle 0
as an input, I would suggest to use the Optional
, i.e. to rewrite this class into something like that:
import java.util.Optional;
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);
}
}
Please note that this will requite updating the tests.
src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java
Outdated
Show resolved
Hide resolved
It is a good idea but I think here -1 represents there are no index of highest set bit. |
Well you need to somehow document that or agree that the index -1 means that there is no such bit. In my opinion using @BamaCharanChhandogi I would still insist on this suggestion. |
b98f8c2
to
8b5c7c7
Compare
hey @vil02! Can you check? |
src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java
Outdated
Show resolved
Hide resolved
src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the code: looks good to me.
I am not sure about the doc-strings (which tags to use etc.). I would add some explanation of what HighestSetBit really does (e.g. that we count from right to left). I think it is also worth to mention that this algorithm assumes that the int
is big endian.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java
Outdated
Show resolved
Hide resolved
src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java
Outdated
Show resolved
Hide resolved
c96791d
to
e9580a3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vil02 please review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the current description of the algorithm is adequate and intuitively clear. We don't need to complicate it with further clarifications about endianness.
src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java
Outdated
Show resolved
Hide resolved
@siriak Check! |
Uh oh!
There was an error while loading. Please reload this page.