Add SetBit to bitmanipulation (#4348)
This commit is contained in:
parent
fc693e8b51
commit
29a864b5b3
10
src/main/java/com/thealgorithms/bitmanipulation/SetBit.java
Normal file
10
src/main/java/com/thealgorithms/bitmanipulation/SetBit.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package com.thealgorithms.bitmanipulation;
|
||||||
|
/**
|
||||||
|
* Sets a specific bit to 1
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class SetBit {
|
||||||
|
public static int setBit(int num, int bit) {
|
||||||
|
return num | (1 << bit);
|
||||||
|
}
|
||||||
|
}
|
13
src/test/java/com/thealgorithms/bitmanipulation/SetBit.java
Normal file
13
src/test/java/com/thealgorithms/bitmanipulation/SetBit.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.thealgorithms.bitmanipulation;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class SetBitTest {
|
||||||
|
@Test
|
||||||
|
void testSetBit() {
|
||||||
|
assertEquals(5, SetBit.setBit(4, 0));
|
||||||
|
assertEquals(3, SetBit.setBit(3, 1));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user