Skip to content

refactor: Enhance docs, add more tests in ReverseBits #5859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arguments> 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));
}
}