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

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/com/thealgorithms/bitmanipulation/GetBit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.thealgorithms.bitmanipulation;
/**
* 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.

public static int getBit(int num, int get) {
return (num >> get) & 1;
}
}
Comment on lines +6 to +10
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

13 changes: 13 additions & 0 deletions src/test/java/com/thealgorithms/bitmanipulation/GetBitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.thealgorithms.bitmanipulation;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class GetBitTest {
@Test
public void getBitTest() {
assertEquals(1, GetBit.getBit(7, 1));
assertEquals(0, GetBit.getBit(5, 1));
}
}