Ofir Attia
Ofir Attia

Reputation: 1275

Java Decimal To Binary - Big Numbers to Binary

My program should convert decimal numbers to binary. For big numbers it is giving me a negative number not a binary number. Why is this?

For example, if I supply 2321 I get 100100010001, which is fine. But if I supply 241242141 I get -2127232070093227171.

I can't use strings, arrays, functions. There is another option without define it as string? the output?

import java.util.Scanner;

public class d {

  public static void main(String[] args) {   

    long num = 0;
    long temp = 0L;

    Scanner sc = new Scanner(System.in);
    num = sc.nextLong();

    long place = 1L;
    long output = 0;
    //System.out.print(""+ num%2+ (num%2)%2);
    while(num != 0) {
      temp = num % 2;
      num = num / 2;    

      output += (place*temp);
      place *=10;
    }

    System.out.print(""+output);    
  }
}

Upvotes: 0

Views: 850

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533520

You problem is here

  output += (place*temp);
  place *=10;

this is producing a number which overflows.

A simple alternative is to create a String instead of generating a number you will convert to a String anyway.

StringBuilder output = new StringBuilder();
while(num != 0) {
  output.append(num & 1);
  num >>>= 1;    
}

System.out.print(output.reverse());   

or even

StringBuilder output = new StringBuilder();
for(long num = sc.netLong(); num != 0; num >>>= 1) 
  output.append(num & 1);

System.out.print(output.reverse());   

If you want to use no functions except input or output.

long num = 241242141;
int shift = 63;
while (num >>> shift == 0 && shift > 0) shift--;
for (; shift >= 0; shift--)
    System.out.print((num >>> shift) & 1);

// for comparison only
System.out.println("\n"+Long.toBinaryString(num));

prints

1110011000010001000000011101
1110011000010001000000011101

Upvotes: 3

Aubin
Aubin

Reputation: 14853

With recursion:

public class d {
   static void toBinaryString( long number )
   {
      if( number > 1 ) toBinaryString( number / 2L );
      System.out.print( number % 2L );
   }
   public static void main(String[] args) {
      long num = 241242141L;
      System.out.println( Long.toBinaryString( num ));
      toBinaryString( num );
   }
}

The ouput:

1110011000010001000000011101
1110011000010001000000011101

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 213243

The problem is that, you are storing your Binary Equivalent in a long type, which cannot store such a long values.

You should rather use a StringBuilder and append your remainder - temp in it. Then print it in reverse: -

    StringBuilder builder = new StringBuilder();
    while(num != 0) {
      temp = num % 2;
      num = num / 2;    

      builder.append(temp);
      output += (place*temp);
      place *=10;
    }

    System.out.println(builder.reverse());

If you don't need to use any methods, then just use String Concatenation, and then a loop to print the string in reverse: -

    String builder = "";
    while(num != 0) {
      temp = num % 2;
      num = num / 2;    

      builder += temp;
      output += (place*temp);
      place *=10;
    }

    for (int i = builder.length() - 1; i >= 0; i--) {
        System.out.print(builder.charAt(i));
    }

But, beware, this will create a large number of String objects on Heap. Also, here you are using a charAt method, that you have to use.

Upvotes: 1

Related Questions