Reverse Bits | Practice | GeeksforGeeks

Given a 32 bit number X, reverse its binary form and print the answer in decimal.
Example 1:
Input:
X = 1
Output:
2147483648 
Explanation:
Binary of 1 in 32 bits representation-
00000000000000000000000000000001
Reversing the binary form we get, 
10000000000000000000000000000000,
whose decimal value is 2147483648.
Example 2:
Input:
X = 5
Output:
2684354560 
Explanation:
Binary of 5 in 32 bits representation-
00000000000000000000000000000101
Reversing the binary form we get, 
10100000000000000000000000000000,
whose decimal value is 2684354560.

Your Task:
You don't need to read input or print anything. Your task is to complete the function reversedBits() which takes an Integer X as input and returns the answer.
Expected Time Complexity: O(log(X))
Expected Auxiliary Space: O(1)
Constraints:
0 <= X < 232 
0
vishnu12655 days ago
static Long reversedBits(Long X) {
        Long ans = 0l;
        int i = 0;
        double power = Math.pow(2,31);
        while(X > 0){
           if(X%2 == 1){
               ans = ans + (long)power;
           }
           X = X / 2;
           power = power / 2;
           i++;
        }
        return ans;
    }
0
60mqcs20shivamyadav6 days ago
#Python easy code
def reversedBits(self, X):       # code here        msk='{:032b}'.format(X)       res = msk[::-1]       a = int(res,2)       return a
0
mukeshparihar5391 month ago
JAVA easy Solution :
static Long reversedBits(Long X) {            String str = "";            while(X>0){          long rem = X%2;          str += rem;          X /= 2;      }      while(str.length()!=32){          str += 0;      }            return Long.parseLong(str,2);               }
0
cshubham4391 month ago
EASY JAVA SOLUTION 
long num = X;
        String str = "";
        while(num>0){
            long rem =num%2;
            str = Long.toString(rem)+str;
            num = num/2;
        }
        while(str.length()!=32){
            str = '0'+str;
        }
        long count = 1;
        long ans = 0;
        for(int i = 0; i<str.length();i++){
            if(str.charAt(i)=='1'){
                ans += count;
            }
            count = count*2;
        }
        return ans;
0
shalini15051 month ago
 long long reversedBits(long long X) {    uint32_t reverse=0;    int pos=31;    while(pos>=0&&X)    {        if(X&1)        {            reverse=reverse|(1<<pos);        }        pos--;        X=X>>1;    }    return reverse;   }
-1
aaravrubral2 months ago
very simple steps just go for it
 
 
class Solution {   static Long reversedBits(Long X) {       // code here       long mask=1; //taling a mask 00000000001       long mask1=1;       long reverse=0; // taking reverse mask 000000000000000000       // converting mask=1 to mask = 1000000000000000               for(int i=0;i<31;i++)           mask=mask<<1;           // now playing a reverse for loop                        for(int i=31;i>=0;i--){                            //if mask1 and X(given number) not equal to 0 then               if((X & mask1)!=0){               reverse=reverse | mask;               mask>>=1;               mask1<<=1;               }               // if mask1 and X(given number) equal to 0 then               else{               mask=mask>>1;               mask1=mask1<<1;               }           }                      return reverse;   }
+1
sachinupreti1902 months ago
// This question ruin my 1 hour but finally got it..... 
 
  long long reversedBits(long long X) {       // code here                     string p="",q,ok="";       long long int i;       long long int rem,b=0,base=1;       while(X!=0)       {           rem=X%2;           ok+=to_string(rem);             X/=2;       }           reverse(ok.begin(),ok.end());       if(ok.size()<32)       {       // Here decimal convert's to binary....       for(i=1;i<=(32-ok.size());i++)        {           p+="0";                               }                                    q=p+ok;}       else       q=ok;      // cout<<q<<endl;       reverse(q.begin(),q.end());       // Here i have finded the reverse of 32 bit interger but in string format.        long long int l=31,ans=0;       for(i=0;i<q.size();i++)                     {           if(q[i]=='1')           ans+=pow(2,l);                                       l--;                             }       return ans;                                                                               }
+1
taiphanvan24032 months ago
0.0s/1.2
So easy to implement
long long reversedBits(long long X) {
        long long res=0;
        int i=0;
        while(X){
            res=res+X%2*pow(2,31-i);
            i++;
            X/=2;
        }
        return res;
    }
0
nikhillko022 months ago
def reversedBits(self, X):       # code here        s=""       X = bin(X)       x  = X.replace("0b","")       l=len(x)       n=32-l       for i in range(n):           x="0"+x       lis = list(x)       lis.reverse()       s=s.join(lis)       r=int(s,2)       return r       
+5
zaid473043 months ago
long long reversedBits(long long X) {       long long ans=0;       for(int i=0;i<32;i++){           ans=(ans<<1)|((X>>i)&1);       }       return ans;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.