2
System.out.print("Enter an integer: ");
int number = input.nextInt();

String binary = "";
while(number != 0) {
   if (number % 2 == 0) {
       binary += "0" ;
   } else{
          binary += "1" ;
   }
   number /= 2;
}

System.out.println(binary);

I have trouble understanding what is wrong with my code. I saw there are solutions to this question but they didn't answer the following question that I have : I know that to display the binary the right way I need to change these line: binary += "0"; to this line: binary = "0" + binary;

I just can't understand WHY, why when I wrote that code the output is not reversed as it should be but If I add the lines it prints the right way.

Thanks.

3 Answers 3

3

Your code keeps checking the modulo of 2, which is in fact the least significant bit of the number, then it divides by two to move on to the next bit (move one bit to the right). For example:

29 = 11101
         ^ check modulo of two,
           append "1",
           divide by two
14 =  1110
         ^ check modulo of two,
           append "0",
           divide by two
 7 =   111
         ^ check modulo of two,
           append "1",
           divide by two
 3 =    11
         ^ check modulo of two,
           append "1",
           divide by two
 1 =     1
         ^ check modulo of two,
           append "1",
           divide by two
(stop)

As you can see, the algorithm appends, in order:

  • 1
  • 0
  • 1
  • 1
  • 1

Which is in fact the reverse of the binary representation. So, in other words, you need to add these digits at the beginning of the String:

while(number != 0) {
  if (number % 2 == 0) {
    binary = "0" + binary;
  } else {
    binary = "1" + binary;
  }
  number /= 2;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You are appending the binary digits in reversed order.

The first digit that you append to the String becomes the first character of the String, which means the least significant bit becomes the most significant bit in the output String.

It should be:

String binary = "";
while (number != 0) {
    if (number % 2 == 0) {
        binary = "0" + binary; // the newly appended digit should be to the left  of the
                               // previously appended digits
    } else {
        binary = "1" + binary;
    }
    number /= 2;
}
System.out.println(binary);

Take a simple example:

Let number == 2

First iteration:

number % 2 == 0
    binary = "0" + binary;  // "0" + "" -> "0"
number /= 2; // now number == 1

Second iteration:

number % 2 != 0
    binary = "1" + binary;  // "1" + "0" -> "10"
number /= 2; // now number == 0

Comments

1
String binary = "";
while (number != 0) {
    if (number % 2 == 0) {
        binary = "0" + binary;
    } else {
        binary = "1" + binary;
    }
    number /= 2;
}
System.out.println(binary);

You can add at the front of the binary String.

There is an another way to do so by using the reverse() method.

String binary = "";
while (number != 0) {
    if (number % 2 == 0) {
        binary += "0" ;
    } else {
        binary += "1" ;
    }
    number /= 2;
}
String reverseBinary = new StringBuffer(binary).reverse().toString();
System.out.println(reverseBinary);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.