diff --git a/src/main/java/com/thealgorithms/bitmanipulation/SetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/SetBit.java new file mode 100644 index 00000000..a4854001 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/SetBit.java @@ -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); + } +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/SetBit.java b/src/test/java/com/thealgorithms/bitmanipulation/SetBit.java new file mode 100644 index 00000000..b6d2514e --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/SetBit.java @@ -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)); + } +}