Reverse Bits in C++

Reversing bits means flipping the binary representation of a number from left to right. For a 32-bit unsigned integer, the leftmost bit becomes the rightmost bit and vice versa.

Understanding Bit Reversal

Given a 32-bit number like 00000000000000000000001001110100, we need to reverse it to 00101110010000000000000000000000. The process involves extracting each bit from the original number and placing it in the reversed position.

Algorithm

The bit reversal algorithm works as follows ?

  • Initialize result to 0
  • For each bit position from 31 down to 0:
    • Extract the least significant bit of the input number
    • Shift this bit to its reversed position
    • OR it with the result
    • Right shift the input number by 1
  • Return the result

Python Implementation

def reverse_bits(n):
    result = 0
    for i in range(32):
        # Extract the least significant bit and shift it to position (31-i)
        result |= (n & 1) << (31 - i)
        # Right shift n to process the next bit
        n >>= 1
    return result

# Test with the example
number = 0b00000000000000000000001001110100
reversed_number = reverse_bits(number)
print(f"Original: {number}")
print(f"Reversed: {reversed_number}")
print(f"Binary: {bin(reversed_number)}")
Original: 628
Reversed: 775946240
Binary: 0b101110010000000000000000000000

Alternative Method Using String Manipulation

def reverse_bits_string(n):
    # Convert to 32-bit binary string
    binary_str = format(n, '032b')
    # Reverse the string
    reversed_str = binary_str[::-1]
    # Convert back to integer
    return int(reversed_str, 2)

# Test the alternative method
number = 628  # Same as 0b00000000000000000000001001110100
result = reverse_bits_string(number)
print(f"Using string method: {result}")
Using string method: 775946240

Step-by-Step Example

Let's trace through the bit reversal process for the number 628 ?

def reverse_bits_with_trace(n):
    result = 0
    original_n = n
    print(f"Original number: {n} ({bin(n)})")
    
    for i in range(32):
        bit = n & 1
        result |= bit << (31 - i)
        n >>= 1
        if i < 10 or bit == 1:  # Show first 10 steps and when bit is 1
            print(f"Step {i}: bit={bit}, result={result}")
    
    print(f"Final result: {result}")
    return result

# Trace the reversal
reverse_bits_with_trace(628)
Original number: 628 (0b1001110100)
Step 0: bit=0, result=0
Step 1: bit=0, result=0
Step 2: bit=1, result=536870912
Step 3: bit=0, result=536870912
Step 4: bit=1, result=805306368
Step 5: bit=1, result=939524096
Step 6: bit=1, result=1006632960
Step 7: bit=0, result=1006632960
Step 8: bit=0, result=1006632960
Step 9: bit=1, result=1014021120
Final result: 775946240

Conclusion

Bit reversal can be achieved by extracting each bit and placing it in the mirrored position. The bitwise method is more efficient than string manipulation for performance-critical applications.

Updated on: 2026-03-25T07:17:09+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements