mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +08:00
Enhance docs, add more tests in ReverseBits
(#5859)
This commit is contained in:
@ -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;
|
||||
|
Reference in New Issue
Block a user