Enhance docs, add more tests in ReverseBits (#5859)

This commit is contained in:
Hardik Pawar
2024-10-29 23:47:30 +05:30
committed by GitHub
parent 54567e2ed3
commit fd14016c86
2 changed files with 53 additions and 8 deletions

View File

@ -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 twos 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;