简体   繁体   中英

Digit '0' that appears after 1 at last get lost in binary number while converting from decimal number

I am beginner in Java and learning to convert the number system from one to another. While converting, I found that digit '0' (zero) that appears after 1 at last didn't get assigned to the binary number.

When I convert 63 decimal digit into binary, the result is 111111; which is right and no issue appeared here because the result binary value doesn't contain any zeroes.

When the decimal digit is 42, the result is 10101 which was supposed to be 101010 ie the '0' that comes after 1 at last didn't get assigned.

And when I convert decimal digit 64 to binary, the result which was supposed to appear 1000000 came to be 1 ie the all '0' (zeroes) after 1 gets lost.

Does this code means the zeroes that appears after 1 at last to be insignificant or are there any other issues?

class DecToBin{
    public static void main(String[] args){
    int dec = 64;
    int bin=0,rem=0;
    while(dec!=0){
        rem=dec%2;
        bin=bin*10+rem;
        dec=dec/2;
    }
    System.out.println("In Binary = "+bin);
  }
}

The trouble is you are adding 0 and not really building the right number you could use a String to store the digit and it will be better to use a StringBuilder and do like this :

public static void main(String[] args) {
    int dec = 64;
    StringBuilder sb = new StringBuilder();
    int  rem = 0;
    while (dec != 0) {
        rem = dec % 2;
        sb.insert(0,rem);
        dec = dec / 2;
    }
    System.out.println("In Binary = " + sb.toString());
}

But you can use Integer.toString(int i, int radix) . What you are trying to do already exist in Java :

public static void main(String[] args) {
    int dec = 64;
    System.out.println("In Binary = " + Integer.toString(dec,2));
}

The problem is that you're converting backwards, and the integer 010101 is the same as 10101. Try with something like 32+3, which is supposed to be 100011 but you get 110001.

Furthermore, a string-concatenation approach has extremely low performance and uses a lot of memory. Using a StringBuilder solves that.

int dec = 35;
// 32 = Most digits you'll ever need with an int.
StringBuilder binBuilder = new StringBuilder(32);
while (dec > 0) {
    int bit = dec & 1; // Using this instead of 'remainder' has higher performance.
    binBuilder.insert(0, bit != 0 ? '1' : '0');
    dec >>>= 1; // Using this instead of 'divide' keeps the 32nd bit.
}
String bin = binBuilder.toString();
System.out.println("In Binary = " + bin);

Thank you Mark for suggesting the use of "StringBuilder".

The problem is that when you add any number to 0, the number stays the same. 10 will remain 10,no matter how many times you add 0 to it.

StringBuilder s=new StringBuilder("");
while(dec!=0)
{
    rem=dec%2;
    s.append(rem);
    dec=dec/2;
}
System.out.println("In Binary = "+s.reverse());

The solution which I have provided works like, it starts with an empty string and then keeps on adding a digit to the left of the string.

Integer.toString() //converts an integer to a string 
concat is used to add to strings.

If in your program you need to use that binary number, you can use

int binary_number=Integer.parseInt(s);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM