Inactive Tutor answered 06/16/22
Ovanes G.
asked 06/16/22Conver Reverse to Binary
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in reverse binary. For an integer x, the algorithm is:
As long as x is greater than 0
Output x % 2 (remainder is either 0 or 1)
x = x / 2
Note: The above algorithm outputs the 0's and 1's in reverse order.
Ex: If the input is:
6
the output is:
011
6 in binary is 110; the algorithm outputs the bits in reverse.
Follow
1
Add comment
More
Report
1 Expert Answer
Tutor
New to Wyzant
class Solution{
public static String reverse_binary(int x){
String result = "";
while(x > 0){
result += x%2;
x /= 2;
}
return result;
}
public static void main(String[] args){
int x = Integer.parseInt(args[0]);
System.out.println(reverse_binary(x));
}
}
Still looking for help? Get the right answer, fast.
Ask a question for free
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Find an Online Tutor Now
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.