mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 18:32:56 +08:00
Add Reverse Bits Algo in Bit-Manipulation (#4299)
Co-authored-by: BamaCharanChhandogi <b.c.chhandogi@gmailcom>
This commit is contained in:

committed by
GitHub

parent
07945c7704
commit
251157059c
@ -0,0 +1,20 @@
|
||||
package com.thealgorithms.bitmanipulation;
|
||||
|
||||
/**
|
||||
* Converts any Octal Number to a Binary Number
|
||||
* @author Bama Charan Chhandogi
|
||||
*/
|
||||
|
||||
public class ReverseBits {
|
||||
|
||||
public static int reverseBits(int n) {
|
||||
int result = 0;
|
||||
int bitCount = 32;
|
||||
for (int i = 0; i < bitCount; i++) {
|
||||
result <<= 1; // Left shift the result to make space for the next bit
|
||||
result |= (n & 1); // OR operation to set the least significant bit of result with the current bit of n
|
||||
n >>= 1; // Right shift n to move on to the next bit
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user