Skip to content

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
merged 19 commits into from
Sep 5, 2023

Conversation

BamaCharanChhandogi
Copy link
Member

@BamaCharanChhandogi BamaCharanChhandogi commented Aug 25, 2023

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.

@BamaCharanChhandogi
Copy link
Member Author

BamaCharanChhandogi commented Aug 28, 2023

If I do this
// Handle negative numbers by converting to their absolute value
if (num < 0) {
num = Math.abs(num);
}

OR(No negative number allow)

if(num<=0){
return -1;
}

Copy link
Member

@vil02 vil02 left a 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.

@BamaCharanChhandogi
Copy link
Member Author

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
Copy link
Member

vil02 commented Aug 28, 2023

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.

@BamaCharanChhandogi
Copy link
Member Author

BamaCharanChhandogi commented Aug 30, 2023

hey @vil02! Can you check?

@siriak siriak requested a review from vil02 August 31, 2023 15:25
Copy link
Member

@vil02 vil02 left a 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.

@siriak
Copy link
Member

siriak commented Sep 2, 2023

@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

Copy link
Member

@vil02 vil02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code and the tests look good.

Please rebase before the merging: after having #4343, newline symbols at the end of each file are required.

@siriak please comment on the doc-strings.

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vil02 please review

siriak
siriak previously approved these changes Sep 4, 2023
Copy link
Member

@siriak siriak left a 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.

@BamaCharanChhandogi
Copy link
Member Author

@siriak Check!

@siriak siriak enabled auto-merge (squash) September 5, 2023 20:17
@siriak siriak merged commit fc693e8 into TheAlgorithms:master Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants