Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Reverse Bits problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Reverse Bits problem solution, we need to reverse bits of a given 32-bit unsigned integer.

Leetcode Reverse Bits problem solution

Leetcode Reverse Bits problem solution in Python.

class Solution:
    # @param n, an integer
    # @return an integer
    def reverseBits(self, n):
        reversed_n = 0
        for i in range(32):
            reversed_n = reversed_n << 1 | ((n >> i) & 1)
        return reversed_n

Reverse Bits problem solution in Java.

public class Solution {
    public int reverseBits(int n) {
        Integer j=31;
        Integer i=0;
        Integer val = n;
        while(i<j){
            if(((val>>i)&1)!=((val>>j)&1)){
                val^=((1<<i)|(1<<j));
            }
            ++i;
            --j;
        }
        return val;
    }
}

Problem solution in C++.

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        string s;
        while(n!=0)
        {
            s+=to_string(n%2);
            n/=2;
        }
        
        string temp(32-s.size(),0);
        s+=temp;
        //cout<<s<<endl;
        uint32_t r=0;
        for(int i=s.size()-1;i>=0;i--)
        {
            if(s[i]=='1')
                r+=pow(2,s.size()-i-1);;
 
        }
        
        return r;
    }
};

Problem solution in C.

uint32_t reverseBits(uint32_t n) {

uint32_t revern;

if (n == 0)
{
    return revern=0;
}
else
{
for(int i =0; i<32; i++)
{
   if (n&(1<<i))
   {
     revern=revern+(0x80000000>>i);
   }
}

return revern;
}
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes