简体   繁体   English

将大的 2^63 十进制转换为二进制

[英]Convert a large 2^63 decimal to binary

I need to convert a large decimal to binary how would I go about doing this?我需要将大十进制转换为二进制我将如何 go 这样做? Decimal in question is this 3324679375210329505有问题的小数是 3324679375210329505

You may want to go forBigDecimal .您可能希望 go 用于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. BigDecimal 由任意精度的 integer 未缩放值和 32 位 integer 比例组成。BigDecimal class 提供算术运算、舍入和比例转换、散列操作、格式转换等操作。 The toString() method provides a canonical representation of a BigDecimal. toString()方法提供了 BigDecimal 的规范表示。

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

How about:怎么样:

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

A bit pointless, but here is a solution in C:有点无意义,但这是 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);
}

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:如果您想要快速处理负数的东西(比Long.toString(n, 2)快 50% 以上,比BigInteger.toString(2)快 150-400%),请尝试以下操作:

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):如果您想要long的实际 Two's Compliment 二进制表示(带前导 1 或 0):

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

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.如果甚至将 0 推入堆栈,如果奇数将 1 推入堆栈,则一旦十进制数达到 1。您可以从堆栈中弹出每个值并打印每个值。

Here is a very inefficient block of code for reference.这是一个非常低效的代码块供参考。 You will probably have to use long instead of integer.您可能必须使用 long 而不是 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;

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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