Repository: TheAlgorithms/Java
Pull request #4415 (2023-09-27 08:18:31)

Add SingleBitOperations
#4350 as its not done yet

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


vil02: > Why does the programm need this private constructor?

Your class has only `static` methods. Therefore it does not really make sense to have objects of this class. Marking the default constructor as `private` (and not having other ones) makes it impossible to create objects of that class. Without this, one could still write something like `var obj = SingleBitOperator();`, but as said earlier, it does not really make sense.

Also JaCoCo will show now _100%_ coverage for your file.


lukasb1b: Ok that does make sense
vil02: @lukasb1b please have a look at [this comment](https://github.com/TheAlgorithms/Java/pull/4415#pullrequestreview-1649293416) about renaming.
lukasb1b: @vil02 changed the naming

Patch:
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);
+    }
+}


Test patch:
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));
+    }
+}