-
Notifications
You must be signed in to change notification settings - Fork 20k
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
Set bit #4356
Changes from all commits
0236fe9
5c9afc3
9fa41d5
7078776
f15d798
1308e0c
952a8b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||||||||||||||||||||||||||||||||
public static int getBit(int num, int get) { | ||||||||||||||||||||||||||||||||
return (num >> get) & 1; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
Comment on lines
+6
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In any case such functionality will be needed.
Suggested change
I am not sure what is the best way to make it accept any Please add tests for the missing branches. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's stick to int32 then |
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)); | ||
} | ||
} |
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.
This is a very good start for the
BitArray
class.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 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.