Skip to content

Commit fd14016

Browse files
authored
Enhance docs, add more tests in ReverseBits (#5859)
1 parent 54567e2 commit fd14016

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Converts any Octal Number to a Binary Number
4+
* This class provides a method to reverse the bits of a 32-bit integer.
5+
* Reversing the bits means that the least significant bit (LSB) becomes
6+
* the most significant bit (MSB) and vice versa.
7+
*
8+
* Example:
9+
* Input (binary): 00000010100101000001111010011100 (43261596)
10+
* Output (binary): 00111001011110000010100101000000 (964176192)
11+
*
12+
* Time Complexity: O(32) - A fixed number of 32 iterations
13+
* Space Complexity: O(1) - No extra space used
14+
*
15+
* Note:
16+
* - If the input is negative, Java handles it using two’s complement representation.
17+
* - This function works on 32-bit integers by default.
18+
*
519
* @author Bama Charan Chhandogi
620
*/
7-
821
public final class ReverseBits {
922
private ReverseBits() {
1023
}
1124

25+
/**
26+
* Reverses the bits of a 32-bit integer.
27+
*
28+
* @param n the integer whose bits are to be reversed
29+
* @return the integer obtained by reversing the bits of the input
30+
*/
1231
public static int reverseBits(int n) {
1332
int result = 0;
1433
int bitCount = 32;

src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java

+32-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,40 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
69

710
class ReverseBitsTest {
811

9-
@Test
10-
void testReverseBits() {
11-
assertEquals(0, ReverseBits.reverseBits(0));
12-
assertEquals(-1, ReverseBits.reverseBits(-1));
13-
assertEquals(964176192, ReverseBits.reverseBits(43261596));
12+
@ParameterizedTest
13+
@MethodSource("provideTestCases")
14+
void testReverseBits(int input, int expected) {
15+
assertEquals(expected, ReverseBits.reverseBits(input));
16+
}
17+
18+
private static Stream<Arguments> provideTestCases() {
19+
return Stream.of(
20+
// Edge case: All bits are 0
21+
Arguments.of(0, 0),
22+
23+
// Edge case: All bits are 1 (Two’s complement representation of -1)
24+
Arguments.of(-1, -1),
25+
26+
// Case with random number 43261596
27+
Arguments.of(43261596, 964176192),
28+
29+
// Case with maximum positive value for 32-bit integer
30+
Arguments.of(Integer.MAX_VALUE, -2),
31+
32+
// Case with minimum value (all bits 1 except the sign bit)
33+
Arguments.of(Integer.MIN_VALUE, 1),
34+
35+
// Case with a single bit set (2^0 = 1)
36+
Arguments.of(1, Integer.MIN_VALUE),
37+
38+
// Case with alternating bits: 0b101010...10 (in binary)
39+
Arguments.of(0xAAAAAAAA, 0x55555555));
1440
}
1541
}

0 commit comments

Comments
 (0)