Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
preritasaini1 authored Oct 29, 2024
2 parents 4a04eff + fd14016 commit a0573a9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
23 changes: 21 additions & 2 deletions src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java
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));
}
}

0 comments on commit a0573a9

Please sign in to comment.