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
14 Answers
+ 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);
}
+ 2
Show your code, what language?
+ 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🤟
+ 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
+ 1
Martin Taylor not with in built methods to convert directly to binary.
0
I forgot to post my code. Please check I have uploaded it.
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🤟
0
But where I am going wrong in my code
0
ChaoticDawg please check
0
Ok just wait . I will let you in soon
0
ChaoticDawg still its not working
0
Martin Taylor I want to go with other logic.
0
vinay kumar singh look this may help You...
https://code.sololearn.com/cc78yPP0F0bm/?ref=app
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.