2

I need to convert a large decimal to binary how would I go about doing this? Decimal in question is this 3324679375210329505

Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • 1
    If you have to do this yourself instead of using library functions, I recently reworked my stackoverflow answer on [a similar question](http://stackoverflow.com/q/5372279/600500) into a [blog entry](http://paul-ebermann.tumblr.com/post/6445203482/big-numbers-selfmade-part-10-14-converting-between). – Paŭlo Ebermann Jun 14 '11 at 00:26
  • 2
    The number ends with 5, it will not be 2^anything. – Paŭlo Ebermann Jun 14 '11 at 00:47
  • Hmm, I need to go find that "Why should I learn binary" question from a few weeks ago and point it here. – Bill K Jun 14 '11 at 00:59
  • If you just want to convert one number, you can do it from the command line using the venerable UNIX / LINUX `dc` tool. – Stephen C Jun 14 '11 at 01:05
  • @Stephen: I just did it: `3324679375210329505 2 o p` gives `10111000100011101000100100011011011111011110101011010110100001`. – Paŭlo Ebermann Jun 14 '11 at 01:19
  • Err, `long l = 332467...;`? What's is the question? Where is your attempt? – user207421 Jan 29 '19 at 00:59

6 Answers6

5

You may want to go for BigDecimal.

A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale.The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal.

new BigDecimal("3324679375210329505").toString(2);
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
5

How about:

String binary = Long.toString(3324679375210329505L, 2);
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

http://www.wikihow.com/Convert-from-Decimal-to-Binary

The Evil Greebo
  • 7,013
  • 3
  • 28
  • 55
  • -1 : Always use the built-in API's before reinventing the wheel and writing your own code. See @roadrunner's (correct) answer. – Bohemian Jun 14 '11 at 01:36
  • 1
    @Bohemian: "Always" even if it's a trivial two-liner and the in-built solution is quite inefficient? I'm sure you agree that that's a bit of a generalization (not that I don't agree generally) :) – Voo Jun 14 '11 at 01:45
  • At the time I wrote this, the question has one tag only: "Binary". Given the lack of context, I went for the general "how to understand the algorithm" answer. – The Evil Greebo Jun 14 '11 at 02:20
0

A bit pointless, but here is a solution in C:

void to_binary(unsigned long long n)
{
    char str[65], *ptr = str + 1;
    str[0] = '\n';
    do{
        *ptr++ = '0' + (n&1);
    } while(n >>= 1);
    while(ptr > str)
        putc(*--ptr, stdout);
}

For the example, it prints out:

    10111000100011101000100100011011011111011110101011010110100001

EDIT: And if you don't mind leading zeros....

void to_binary(unsigned long long n)
{
    do{ putc('0' + (n>>63), stdout); } while(n <<= 1);
}
Mikola
  • 9,176
  • 2
  • 34
  • 41
0

If you want something fast (over 50% faster than Long.toString(n, 2) and 150-400% faster than BigInteger.toString(2)) that handles negative numbers the same as the built-ins, try the following:

static String toBinary (long n) {
    int neg = n < 0 ? 1 : 0;
    if(n < 0) n = -n;
    int pos = 0;
    boolean[] a = new boolean[64];
    do {
        a[pos++] = n % 2 == 1;
    } while ((n >>>= 1) != 0);
    char[] c = new char[pos + neg];
    if(neg > 0) c[0] = '-';
    for (int i = 0; i < pos; i++) {
        c[pos - i - 1 + neg] = a[i] ? '1' : '0';
    }
    return new String(c);
}

If you want the actual Two's Compliment binary representation of the long (with leading 1s or 0s):

static String toBinaryTC (long n) {
    char[] c = new char[64];
    for(int i = 63; i >= 0; i--, n >>>= 1) {
        c[i] = n % 2 != 0 ? '1' : '0';          
    }
    return new String(c);        
}
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
0

I would use a Stack! Check if your decimal number is even or odd, if even push a 0 to the stack and if its odd push a 1 to the stack. Then once your decimal number hits 1, you can pop each value from the stack and print each one.

Here is a very inefficient block of code for reference. You will probably have to use long instead of integer.

import java.util.Stack;

public class DecBinConverter {

Stack<Integer> binary;

public DecBinConverter()
{
    binary = new Stack<Integer>();
}

public int dec_Bin(int dec)
{
    if(dec == 1)
    {
        System.out.print(1);
        return 0;
    }
    if(dec == 0)
    {
        System.out.print(0);
        return 0;
    }
        if((dec%2) == 0)
        {
            binary.push(0);
            dec = dec/2;
        }
        else
        {
            binary.push(1);
            dec = dec/2;
        }   
        while(dec != 1)
        {

            if((dec%2) == 0)
            {
                binary.push(0);
                dec = dec/2;

            }
            else
            {
                binary.push(1);
                dec = dec/2;
            }   
        }
        if((dec%2) == 0)
        {
            binary.push(0);
            dec = dec/2;
        }
        else
        {
            binary.push(1);
            dec = dec/2;

        }
        int x = binary.size();
        for(int i = 0; i < x; i++)
        {
            System.out.print(binary.pop());
        }
        return 0;

}

}
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
Cbin
  • 13
  • 4