From 9c81fc995db8eb0e9fa63ee3b2ab270a7bd682c2 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sat, 12 Aug 2023 15:51:01 +0530 Subject: [PATCH 1/3] add Reverse Bits Algo in Bit-Manipulation --- .../bitmanipulation/ReverseBits.java | 24 +++++++++++++++++++ .../bitmanipulation/ReverseBitsTest.java | 14 +++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java diff --git a/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java new file mode 100644 index 000000000000..86122b5ae191 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java @@ -0,0 +1,24 @@ +package com.thealgorithms.bitmanipulation; + +/** + * Converts any Octal Number to a Binary Number + * @author Bama Charan Chhandogi + */ + +public class ReverseBits { + + public static int reverseBits(int n) { + int result = 0; + int bitCount = 32; // Assuming integers are 32 bits in Java + + for (int i = 0; i < bitCount; i++) { + result <<= 1; // Left shift the result to make space for the next bit + result |= (n & 1); // OR operation to set the least significant bit of result with the current bit of n + n >>= 1; // Right shift n to move on to the next bit + } + + return result; + } +} + + diff --git a/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java b/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java new file mode 100644 index 000000000000..fefa103794ca --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java @@ -0,0 +1,14 @@ +package com.thealgorithms.bitmanipulation; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class ReverseBitsTest { + + @Test + void testReverseBits() { + assertEquals(0, ReverseBits.reverseBits(0)); + assertEquals(-1, ReverseBits.reverseBits(-1)); + assertEquals(964176192, ReverseBits.reverseBits(43261596)); + } +} From 22777e4b0127a36310b81187b99666b0a5561a6b Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sat, 12 Aug 2023 15:59:52 +0530 Subject: [PATCH 2/3] linter solved --- .../com/thealgorithms/bitmanipulation/ReverseBits.java | 8 ++------ .../thealgorithms/bitmanipulation/ReverseBitsTest.java | 3 ++- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java index 86122b5ae191..9d99ef9472e5 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java @@ -9,16 +9,12 @@ public class ReverseBits { public static int reverseBits(int n) { int result = 0; - int bitCount = 32; // Assuming integers are 32 bits in Java - + int bitCount = 32; // Assuming integers are 32 bits in Java for (int i = 0; i < bitCount; i++) { result <<= 1; // Left shift the result to make space for the next bit result |= (n & 1); // OR operation to set the least significant bit of result with the current bit of n n >>= 1; // Right shift n to move on to the next bit } - return result; } -} - - +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java b/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java index fefa103794ca..730e345686b6 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java @@ -1,8 +1,9 @@ package com.thealgorithms.bitmanipulation; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + class ReverseBitsTest { @Test From 4d71394e7c6d44eee877d4734546275c89bbd1f4 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sat, 12 Aug 2023 16:03:36 +0530 Subject: [PATCH 3/3] linter solved2 --- .../java/com/thealgorithms/bitmanipulation/ReverseBits.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java index 9d99ef9472e5..58359598266e 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java @@ -9,7 +9,7 @@ public class ReverseBits { public static int reverseBits(int n) { int result = 0; - int bitCount = 32; // Assuming integers are 32 bits in Java + int bitCount = 32; for (int i = 0; i < bitCount; i++) { result <<= 1; // Left shift the result to make space for the next bit result |= (n & 1); // OR operation to set the least significant bit of result with the current bit of n