Reverse Bits LeetCode Solution

Last updated on July 15th, 2024 at 05:24 am

Here, We see Reverse Bits LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

List of all LeetCode Solution

Topics

Bit-Manipulation

Companies

Airbnb, Apple

Level of Question

Easy

Reverse Bits LeetCode Solution

Reverse Bits LeetCode Solution

Problem Statement

Reverse bits of a given 32 bits unsigned integer.

Note:

  • Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned.
  • In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.
Example 1:
Input: n = 00000010100101000001111010011100
Output:    964176192 (00111001011110000010100101000000)
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.

Example 2:
Input: n = 11111111111111111111111111111101
Output:   3221225471 (10111111111111111111111111111111)
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.

1. Reverse Bits Leetcode Solution C++

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t res = 0;
        for (int i = 0; i < 31; i++) {
            res = (n % 2) + res << 1;
            n >>= 1;
        }
        return res + n % 2;
    }
};

1.1 Explanation

  • Variables: res is initialized to 0 to store the reversed bits, and n is the input number whose bits are to be reversed.
  • Iteration: The loop runs 31 times (since a 32-bit integer has 32 bits, and the MSB (Most Significant Bit) remains unchanged in this implementation).
  • Bit Manipulation:
    • res = (n % 2) + res << 1; calculates the reverse bit by bit:
      • (n % 2) gets the least significant bit of n.
      • res << 1 shifts res left by 1 bit to make space for the next bit.
      • n >>= 1 shifts n right by 1 bit to process the next bit in the next iteration.
  • Return: res + n % 2 adds the MSB (Most Significant Bit) of n to res to complete the reversal.

1.2 Time Complexity

  • O(1), because the loop runs a constant number of times (31 iterations).

1.3 Space Complexity

  • O(1), since only a constant amount of extra space is used.

2. Reverse Bits Leetcode Solution Java

public class Solution {
    public int reverseBits(int n) {
        int res=0;
    for(int i=0;i&lt;32;i++){
    	res= ( res &lt;&lt; 1 ) | ( n &amp; 1 );         
    	n = n &gt;&gt; 1;                  
    }
    return res;
    }
}

2.1 Explanation

  • Variables: res starts as 0 to store the reversed bits, and n is the input number whose bits are to be reversed.
  • Iteration: The loop runs 32 times, covering all 32 bits of the integer.
  • Bit Manipulation:
    • (res << 1) | (n & 1); shifts res left by 1 bit and then ORs it with the least significant bit of n.
    • n >>= 1; shifts n right by 1 bit to process the next bit in the next iteration.
  • Return: res holds the reversed bits after completing the loop.

2.2 Time Complexity

  • O(1), because the loop runs a constant number of times (32 iterations).

2.3 Space Complexity

  • O(1), since only a constant amount of extra space is used.

3. Reverse Bits Leetcode Solution JavaScript

var reverseBits = function(n) {
    return parseInt([...`${Number(n).toString(2)}`.padStart(32,0)].reverse().join(''),2)
};

3.1 Explanation

  • Conversion: Number(n).toString(2) converts n to a binary string representation.
  • padStart(32, 0) ensures the binary string is padded to 32 bits with leading zeros.
  • reverse() reverses the order of bits.
  • join(”) converts the array of bits back into a string.
  • parseInt(…, 2) converts the reversed binary string back into a number.

3.2 Time Complexity

  • O(1), because the operations (string conversion, reversal, and parsing) are done in constant time relative to the fixed size (32 bits).

3.3 Space Complexity

  • O(1), since only a constant amount of extra space is used.

4. Reverse Bits Leetcode Solution Python

class Solution:
    def reverseBits(self, n):
        oribin='{0:032b}'.format(n)
        reversebin=oribin[::-1]
        return int(reversebin,2)

4.1 Explanation

  • Conversion: ‘{0:032b}’.format(n) converts n to a 32-bit binary string.
  • [::-1] reverses the order of bits in the string.
  • int(…, 2) converts the reversed binary string back into an integer.

4.2 Time Complexity

  • O(1), because the operations are performed in constant time relative to the fixed size (32 bits).

4.3 Space Complexity

  • O(1), since only a constant amount of extra space is used.
Scroll to Top