From e5d33f35651e8b46ba2318dbd934dc3eeb81507b Mon Sep 17 00:00:00 2001 From: Lukas <142339568+lukasb1b@users.noreply.github.com> Date: Sat, 30 Sep 2023 10:06:45 +0200 Subject: [PATCH] Add SingleBitOperations (#4415) * SingleBitOperators * Tests * Update SingleBitOperatorTest.java * Update SingleBitOperators.java * Update SingleBitOperators.java * Update SingleBitOperators.java * Update SingleBitOperatorTest.java * deliting files * Update SingleBitOperators.java * Update SingleBitOperatorTest.java * Update SingleBitOperators.java * Update SingleBitOperators.java * Update SingleBitOperatorTest.java * Update SingleBitOperatorTest.java * Update and rename SingleBitOperators.java to SingleBitOperator.java * Update SingleBitOperatorTest.java * Update SingleBitOperator.java * Update SingleBitOperator.java * Update SingleBitOperator.java * style: declare private default constructor * fix: remove `SetBitTest.java` * Update and rename SingleBitOperator.java to SingleBitOperations.java * Update SingleBitOperatorTest.java * Update SingleBitOperations.java * Update and rename SingleBitOperatorTest.java to SingleBitOperationsTest.java --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../bitmanipulation/ClearBit.java | 11 ------ .../thealgorithms/bitmanipulation/SetBit.java | 10 ------ .../bitmanipulation/SingleBitOperations.java | 34 +++++++++++++++++++ .../bitmanipulation/ClearBitTest.java | 13 ------- .../bitmanipulation/SetBitTest.java | 13 ------- .../SingleBitOperationsTest.java | 32 +++++++++++++++++ 6 files changed, 66 insertions(+), 47 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/bitmanipulation/ClearBit.java delete mode 100644 src/main/java/com/thealgorithms/bitmanipulation/SetBit.java create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java delete mode 100644 src/test/java/com/thealgorithms/bitmanipulation/ClearBitTest.java delete mode 100644 src/test/java/com/thealgorithms/bitmanipulation/SetBitTest.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java diff --git a/src/main/java/com/thealgorithms/bitmanipulation/ClearBit.java b/src/main/java/com/thealgorithms/bitmanipulation/ClearBit.java deleted file mode 100644 index c863c46c..00000000 --- a/src/main/java/com/thealgorithms/bitmanipulation/ClearBit.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.thealgorithms.bitmanipulation; -/** - * Clears the bit located at clear from num - */ - -public class ClearBit { - public static int clearBit(int num, int clear) { - int mask = ~(1 << clear); - return num & mask; - } -} diff --git a/src/main/java/com/thealgorithms/bitmanipulation/SetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/SetBit.java deleted file mode 100644 index a4854001..00000000 --- a/src/main/java/com/thealgorithms/bitmanipulation/SetBit.java +++ /dev/null @@ -1,10 +0,0 @@ -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/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java b/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java new file mode 100644 index 00000000..b41aeca1 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java @@ -0,0 +1,34 @@ +package com.thealgorithms.bitmanipulation; + +/* + * Author: lukasb1b (https://github.com/lukasb1b) + */ + +public final class SingleBitOperations { + private SingleBitOperations() { + } + /** + * Flip the bit at position 'bit' in 'num' + */ + public static int flipBit(final int num, final int bit) { + return num ^ (1 << bit); + } + /** + * Set the bit at position 'bit' to 1 in the 'num' variable + */ + public static int setBit(final int num, final int bit) { + return num | (1 << bit); + } + /** + * Clears the bit located at 'bit' from 'num' + */ + public static int clearBit(final int num, final int bit) { + return num & ~(1 << bit); + } + /** + * Get the bit located at 'bit' from 'num' + */ + public static int getBit(final int num, final int bit) { + return ((num >> bit) & 1); + } +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/ClearBitTest.java b/src/test/java/com/thealgorithms/bitmanipulation/ClearBitTest.java deleted file mode 100644 index 60f73e9d..00000000 --- a/src/test/java/com/thealgorithms/bitmanipulation/ClearBitTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.thealgorithms.bitmanipulation; - -import static org.junit.jupiter.api.Assertions.*; - -import org.junit.jupiter.api.Test; - -public class ClearBitTest { - @Test - public void clearBitTest() { - assertEquals(5, ClearBit.clearBit(7, 1)); - assertEquals(5, ClearBit.clearBit(5, 1)); - } -} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/SetBitTest.java b/src/test/java/com/thealgorithms/bitmanipulation/SetBitTest.java deleted file mode 100644 index b6d2514e..00000000 --- a/src/test/java/com/thealgorithms/bitmanipulation/SetBitTest.java +++ /dev/null @@ -1,13 +0,0 @@ -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)); - } -} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java b/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java new file mode 100644 index 00000000..a6bb7668 --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java @@ -0,0 +1,32 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class SingleBitOperationsTest { + + @Test + public void flipBitTest() { + assertEquals(1, SingleBitOperations.flipBit(3, 1)); + assertEquals(11, SingleBitOperations.flipBit(3, 3)); + } + + @Test + public void setBitTest() { + assertEquals(5, SingleBitOperations.setBit(4, 0)); + assertEquals(4, SingleBitOperations.setBit(4, 2)); + } + + @Test + public void clearBitTest() { + assertEquals(5, SingleBitOperations.clearBit(7, 1)); + assertEquals(5, SingleBitOperations.clearBit(5, 1)); + } + + @Test + public void getBitTest() { + assertEquals(0, SingleBitOperations.getBit(6, 0)); + assertEquals(1, SingleBitOperations.getBit(7, 1)); + } +}