0

How to reverse a binary number in my program?

I have first converted decimal num into binary then trying to reverse a binary num which I have obtained. Take example as 8 its binary coming in my program is 0001 but it should be 1000. That's why I want to reverse it. https://code.sololearn.com/cmp3Axi3ObzS/?ref=app

28th May 2020, 7:38 AM
vinay kumar singh
vinay kumar singh - avatar
18 Respostas
+ 3
Here's a recursive bitshifting method that will work for either a positive or negative int. private static void decToBin(int num) { if (num == 0) return; decToBin(num >>> 1); System.out.print(num & 1); }
29th May 2020, 5:52 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Show your code, what language?
28th May 2020, 7:41 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Just check there is a code mistake between 20th line to 25th line. As I can't tell you the mistake according to sololearn policies. But I can help you Just check your every code mistake by knowing the facts from here https://www.codejava.net/coding/10-common-mistakes-every-beginner-java-programmer-makes Just rock the code nowđŸ€Ÿ
28th May 2020, 7:58 AM
Piyush
Piyush - avatar
+ 2
vinay kumar singh you're not storing your zeros and ones correctly in your while loop. You need to put them in an array, a data structure, or append them to the front of a String. https://www.javatpoint.com/java-decimal-to-binary Scanner in = new Scanner(System.in); System.out.println("\nEnter num;"); int num = in.nextInt(); int rev = 0; // remove other variables String revnum = ""; // change to String while(num>0) { revnum = (num%2) + revnum; // append to front num = num/2; } System.out.println(revnum); // output binary // remove 2nd while loop
28th May 2020, 8:08 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Martin Taylor not with in built methods to convert directly to binary.
29th May 2020, 4:52 AM
vinay kumar singh
vinay kumar singh - avatar
0
I forgot to post my code. Please check I have uploaded it.
28th May 2020, 7:45 AM
vinay kumar singh
vinay kumar singh - avatar
0
You can just covert your binary contact as you can also learn about binary system from here - https://www.electronics-tutorials.ws/binary/bin_2.html I think that this information will be enough. Just rock the codeđŸ€Ÿ
28th May 2020, 7:50 AM
Piyush
Piyush - avatar
0
But where I am going wrong in my code
28th May 2020, 7:55 AM
vinay kumar singh
vinay kumar singh - avatar
0
ChaoticDawg please check
28th May 2020, 7:55 AM
vinay kumar singh
vinay kumar singh - avatar
0
Ok just wait . I will let you in soon
28th May 2020, 7:56 AM
Piyush
Piyush - avatar
0
ChaoticDawg still its not working
28th May 2020, 12:20 PM
vinay kumar singh
vinay kumar singh - avatar
0
Martin Taylor I want to go with other logic.
28th May 2020, 12:21 PM
vinay kumar singh
vinay kumar singh - avatar
29th May 2020, 1:39 PM
Syed Waseem
Syed Waseem - avatar
0
To invert binary numbers within your program, follow these steps. Convert a decimal number into binary. Store this binary number in a variable. Initialize another new variable with value 0. Loop through each bit in order, starting from its rightmost bit and working back. After each bit, shift its value one position left and add it as part of a loop; when completed, this variable will contain reversed binary numbers; for instance if your decimal number 8 corresponds to binary "0001," for instance it could be reversed as follows: Store "0001" into a variable called "binary_num". Initialize another variable named "reversed_num" with 0. Loop through each bit in "binary_num", starting from its rightmost bit and working leftwards. Shift the value of "reversed_num" one position left and add one digit - now its reversed_num is one! For each subsequent bit, shift its value left one bit before adding any more bits - for an end result of "reversed_num = 1000." "Reversed_num" equals 1000; which represents the reversed binary number 0001.
4th May 2023, 9:25 AM
revida