Question

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

OTHER TIPS

How about:

String binary = Long.toString(3324679375210329505L, 2);

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);

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;

}

}

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);        
}

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top