From 7d5dcd69896e86eab6ec27807ad02ad84bee2e0a Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 16 Oct 2024 08:45:44 +0530 Subject: [PATCH 1/2] refactor: Enhance docs, add more tests in `ReverseBits` --- .../bitmanipulation/ReverseBits.java | 29 +++++++++++--- .../bitmanipulation/ReverseBitsTest.java | 38 ++++++++++++++++--- 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java index e8f2930d3afe..db082ce711cf 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java @@ -1,21 +1,40 @@ package com.thealgorithms.bitmanipulation; /** - * Converts any Octal Number to a Binary Number + * This class provides a method to reverse the bits of a 32-bit integer. + * Reversing the bits means that the least significant bit (LSB) becomes + * the most significant bit (MSB) and vice versa. + * + * Example: + * Input (binary): 00000010100101000001111010011100 (43261596) + * Output (binary): 00111001011110000010100101000000 (964176192) + * + * Time Complexity: O(32) - A fixed number of 32 iterations + * Space Complexity: O(1) - No extra space used + * + * Note: + * - If the input is negative, Java handles it using two’s complement representation. + * - This function works on 32-bit integers by default. + * * @author Bama Charan Chhandogi */ - public final class ReverseBits { private ReverseBits() { } + /** + * Reverses the bits of a 32-bit integer. + * + * @param n the integer whose bits are to be reversed + * @return the integer obtained by reversing the bits of the input + */ public static int reverseBits(int n) { int result = 0; 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 - n >>= 1; // Right shift n to move on to the next bit + result <<= 1; + result |= (n & 1); + n >>= 1; } return result; } diff --git a/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java b/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java index 967a89a1ee97..5cfa82355ce7 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java @@ -2,14 +2,40 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; class ReverseBitsTest { - @Test - void testReverseBits() { - assertEquals(0, ReverseBits.reverseBits(0)); - assertEquals(-1, ReverseBits.reverseBits(-1)); - assertEquals(964176192, ReverseBits.reverseBits(43261596)); + @ParameterizedTest + @MethodSource("provideTestCases") + void testReverseBits(int input, int expected) { + assertEquals(expected, ReverseBits.reverseBits(input)); + } + + private static Stream provideTestCases() { + return Stream.of( + // Edge case: All bits are 0 + Arguments.of(0, 0), + + // Edge case: All bits are 1 (Two’s complement representation of -1) + Arguments.of(-1, -1), + + // Case with random number 43261596 + Arguments.of(43261596, 964176192), + + // Case with maximum positive value for 32-bit integer + Arguments.of(Integer.MAX_VALUE, -2), + + // Case with minimum value (all bits 1 except the sign bit) + Arguments.of(Integer.MIN_VALUE, 1), + + // Case with a single bit set (2^0 = 1) + Arguments.of(1, Integer.MIN_VALUE), + + // Case with alternating bits: 0b101010...10 (in binary) + Arguments.of(0xAAAAAAAA, 0x55555555)); } } From c3bcdfde235d6ec852fc386343ad9c2b2183e050 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 29 Oct 2024 10:07:28 +0530 Subject: [PATCH 2/2] Fix --- .../java/com/thealgorithms/bitmanipulation/ReverseBits.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java index db082ce711cf..12c269d9be48 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java @@ -32,9 +32,9 @@ public static int reverseBits(int n) { int result = 0; int bitCount = 32; for (int i = 0; i < bitCount; i++) { - result <<= 1; - result |= (n & 1); - n >>= 1; + 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; }