Skip to content

Set bit #4356

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

Closed
wants to merge 7 commits into from
Closed

Set bit #4356

wants to merge 7 commits into from

Conversation

lukasb1b
Copy link
Contributor

@lukasb1b lukasb1b commented Sep 7, 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.

@lukasb1b
Copy link
Contributor Author

lukasb1b commented Sep 7, 2023

Issue #4350

@lukasb1b
Copy link
Contributor Author

lukasb1b commented Sep 7, 2023

I want to add to the Issue that SetBit is already implemented and ClearBit is in other pullrequest

@siriak
Copy link
Member

siriak commented Sep 7, 2023

@vil02 @BamaCharanChhandogi please review

@BamaCharanChhandogi
Copy link
Member

I want to add to the Issue that SetBit is already implemented and ClearBit is in other pullrequest

Hi, @lukasb1b,
All functions of single bit manipulation should be included in one file named 'Single Bit Manipulation,'. That would be better.

@vil02
Copy link
Member

vil02 commented Sep 7, 2023

* Returns the value of bit from num located at get
*/

public class GetBit {
Copy link
Member

Choose a reason for hiding this comment

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

This is a very good start for the BitArray class.

Copy link
Member

Choose a reason for hiding this comment

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

I suggest adding this as-is and consolidating algorithms into a data structure in a following PR. There are already some other functions implemented in this manner and, as you have mentioned, there are open questions about the design of this BitArray.
@lukasb1b will you be able to consolidate these bit functions into a BitArray in the next PR? If no, somebody else could pick up this task.
In the meantime, we can discuss what we want to use as an underlying type.

Comment on lines +6 to +10
public class GetBit {
public static int getBit(int num, int get) {
return (num >> get) & 1;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

In any case such functionality will be needed.

Suggested change
public class GetBit {
public static int getBit(int num, int get) {
return (num >> get) & 1;
}
}
public final class GetBit {
private GetBit() {
}
public static boolean get(final int data, final int pos) {
if (pos < 0 || pos >= Integer.SIZE) {
throw new ArrayIndexOutOfBoundsException();
}
return ((data >> pos) & 1) > 0;
}
}

I am not sure what is the best way to make it accept any IntegralType - I could not find a convenient way how to get the SIZE.

Please add tests for the missing branches.

Copy link
Member

Choose a reason for hiding this comment

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

Let's stick to int32 then

@lukasb1b
Copy link
Contributor Author

lukasb1b commented Sep 8, 2023

I can add all the SingleBit functions in onte but at first i think we should have a solution for the Problem i personally think a bit array is not as functional but we could do it generic like in the example below. And we add another function for arrays.

public class GetBit{

public static <T extends Number> T getBit(T num, int get) throws Exception {
    if(num instanceof Integer){
        return (T)(Integer.valueOf((num.intValue()>>get) & 1));
    }else{
        throw new Exception();
    }
}

@vil02
Copy link
Member

vil02 commented Sep 8, 2023

I can add all the SingleBit functions in onte but at first i think we should have a solution for the Problem i personally think a bit array is not as functional but we could do it generic like in the example below. And we add another function for arrays.

public class GetBit{

public static <T extends Number> T getBit(T num, int get) throws Exception {
    if(num instanceof Integer){
        return (T)(Integer.valueOf((num.intValue()>>get) & 1));
    }else{
        throw new Exception();
    }
}

As suggested before, let stick to int for now. If you agree with my suggestion, please apply it and add missing tests.

I agree that it is a good idea to have one utilitiy class like BitOperations with public static methods get and set (this will be needed anyway in the BitArray).

@lukasb1b
Copy link
Contributor Author

lukasb1b commented Sep 9, 2023

Hi I closed the request so you can implement it I dont get how you want to do it so I think its quicker if you do it yourself

@lukasb1b lukasb1b closed this Sep 9, 2023
@lukasb1b lukasb1b deleted the SetBit branch October 3, 2023 21:54
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