diff --git a/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java index e8f2930d3afe..12c269d9be48 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java @@ -1,14 +1,33 @@ 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; 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)); } }