Converting 2 ^ 63 binary large numbers to binary

I need to convert a large decimal to binary, how would I do it? This decimal is 3324679375210329505

+3
source share
6 answers

You may need it BigDecimal.

A BigDecimal 32- . BigDecimal , , , , . toString() BigDecimal.

new BigDecimal("3324679375210329505").toString(2);
+5

:

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

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

:

    10111000100011101000100100011011011111011110101011010110100001

EDIT: ....

void to_binary(unsigned long long n)
{
    do{ putc('0' + (n>>63), stdout); } while(n <<= 1);
}
0

- ( 50% , Long.toString(n, 2) 150-400% , BigInteger.toString(2)), , , :

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 binary representation of Two Compliment from 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);        
}
0
source

I would use Stack! Check if your decimal is even or odd, even if you push 0 on the stack, and if it is odd pushing 1 on the stack. Then, as soon as your decimal number reaches 1, you can print each value from the stack and print each of them.

Here is a very inefficient code block for reference. You may 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;

}

}
0
source

All Articles