Java approach with toBinaryString() function


#1

public class Solution {

public long reverse(long a) {
    String s = Long.toBinaryString(a);
    int n1 = s.length();
    for(int i=0;i<32-n1;i++)
    {
        s = "0"+s;
    }
    StringBuffer sb=new StringBuffer();
    sb.append(s);
    StringBuffer sb1= sb.reverse();
    String s1 = String.valueOf(sb1);
    String []str = s1.split("");
    int n=str.length;
    long b=0;
    for(int i=0;i<n;i++)
    {
        b=Long.parseLong(str[i])*(long)Math.pow(2,(n-i-1)) + b;
    }
    return b;
}

}