Skip to content

Commit 29a864b

Browse files
authored
Add SetBit to bitmanipulation (TheAlgorithms#4348)
1 parent fc693e8 commit 29a864b

File tree

2 files changed

+23
-0
lines changed
  • src

2 files changed

+23
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.thealgorithms.bitmanipulation;
2+
/**
3+
* Sets a specific bit to 1
4+
*/
5+
6+
public class SetBit {
7+
public static int setBit(int num, int bit) {
8+
return num | (1 << bit);
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class SetBitTest {
8+
@Test
9+
void testSetBit() {
10+
assertEquals(5, SetBit.setBit(4, 0));
11+
assertEquals(3, SetBit.setBit(3, 1));
12+
}
13+
}

0 commit comments

Comments
 (0)