Add SetBit to bitmanipulation (#4348)

This commit is contained in:
Lukas 2023-09-06 16:46:45 +02:00 committed by GitHub
parent fc693e8b51
commit 29a864b5b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View 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);
}
}

View 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));
}
}