简体   繁体   English

用于打印巨大(超过128位)二进制数的十进制值的算法?

[英]Algorithm for printing decimal value of a huge(over 128bits) binary number?

TLDR, at the bottom :) TLDR,在底部:)

Brief: I am in a process of creating an basic arithmetic library(addition, subtraction, ...) for handling huge numbers. 简介:我正在创建一个基本的算术库(加法,减法,...)来处理大数字。 One of the problem i am facing is printing these huge binary numbers into decimal. 我面临的一个问题是将这些巨大的二进制数字打印成十进制数。

I have huge binary number stored in an array of uint64_t. 我有一个巨大的二进制数存储在uint64_t数组中。 eg 例如

uint64_t a[64] = {0};

Now, the goal is to print the 64*64bits binary number in the console/file as its decimal value. 现在,目标是在控制台/文件中打印64 * 64位二进制数作为其十进制值。

Initial Work: To elaborate the problem I want to describe how I printed hex value. 初步工作:详细说明问题我想描述我如何打印十六进制值。

int i;
int s = 1;
a[1] = (uint64_t)0xFF;
for(i = s; i>= 0; i--)
{
    printf("0x%08llX, ", a[i]);
}

Output: 输出:

0x000000FF, 0x00000000, 

Similarly for printing OCT value I can just take LSB 3 bits from a[64], print decimal equivalent of those bits, 3 bits right shift all the bits of a[64] and keep repeating until all the values of a[64] has been printed. 类似地,对于打印OCT值,我可以从[64]中取出LSB 3位,打印那些位的十进制等效值,3位右移[a]的所有位并继续重复直到a [64]的所有值都有已被打印。 (print in revers order to keep first Oct digit on the right) (以反转顺序打印以保持右侧的第一个十进制数字)

I can print Hex and Oct value of a binary of unlimited size just by repeating this unit algorithm, but I could not find/develop one for Decimal which I can repeat over and over again to print a[64](or something bigger). 我可以通过重复这个单位算法来打印无限大小二进制的十六进制和十月值,但我找不到/开发一个十进制数,我可以一遍又一遍地重复打印[64](或更大的东西)。

What I have thought of: My initial idea was to keep subtracting 我所想到的:我最初的想法是不断减去

max_64 =(uint64)10000000000000000000; //(i.e.10^19)

the biggest multiple of 10 inside uint64_t, from a until the value inside a is smaller than max_64 (which is basically equivalent of rem_64 = a%max_64 ) and print the rem_64 value using 的10内部uint64_t中最大的倍数,从a直到内的值a比max_64(这基本上等效的较小rem_64 = a%max_64 ),并使用打印rem_64值

printf("%019llu",rem_64);

which is the 1st 19 decimal digits of the number a . 这是数字a 19位十进制数字。

Then do an arithmetic operation similar to (not the code): 然后做一个类似于(不是代码)的算术运算:

a = a/max_64; /* Integer division(no fractional part) to remove right most 19 dec digits from 'a' */

and keep repeating and printing 19 decimal digits. 并不断重复和打印19位十进制数字。 (print in such a way that first found 19 digits are on the right, then next 19 digits on its left and so on...). (以这样的方式打印,首先找到19位数字在右边,然后是左边的19位数字,依此类推......)。

The problem is this process is to long and I don't want to use all these to just print the dec value. 问题是这个过程很长,我不想使用所有这些来打印dec值。 And was looking for a process which avoids using these huge time consuming arithmetic operations. 并且正在寻找一种避免使用这些耗时的算术运算的过程。

What I believe is that there must be a way to print huge size just by repeating an algorithm (similar to how Hex and Oct can be printed) and I hope someone could point me to the right direction. 我认为必须有一种方法可以通过重复算法来打印巨大的尺寸(类似于Hex和Oct的打印方式),我希望有人可以指出我正确的方向。

What my library can do(so far): 我的图书馆可以做什么(到目前为止):

  • Add (Using Full-Adder) 添加(使用全加器)
  • Sub (Using Full-subtractor) Sub(使用全减法器)
  • Compare (by comparing array size and comparing array elements) 比较(通过比较数组大小和比较数组元素)
  • Div (Integer division, no fractional part) Div(整数除法,无小数部分)
  • Modulus (%) 模数(%)
  • Multiplication (basically adding from several times :( ) 乘法(基本上是几次加法:()

I will write code for other operations if needed, but I would like to implement the printing function independent of the library if possible. 如果需要,我会为其他操作编写代码,但如果可能的话,我想实现独立于库的打印功能。

Consider the problem like this: You have been given a binary number X of n bits (1<=n<=64*64) you have to print out X in decimal. 考虑这样的问题:你得到了一个n位的二进制数X(1 <= n <= 64 * 64)你必须以十进制打印出X. You can use existing library if absolutely needed but better if unused. 如果绝对需要,您可以使用现有库,但如果未使用则更好。

TLDR: Any code, reference or unit algorithm which I can repeat for printing decimal value of a binary of too big and/or unknown size would be much helpful. TLDR:任何代码,参考或单位算法,我可以重复打印太大和/或未知大小的二进制的十进制值将是非常有帮助的。 Emphasis on algorithm ie I don't need a code if some one could describe a process I will be able to implement it. 强调算法,即如果有人可以描述一个我将能够实现它的过程,我就不需要代码。 Thanks in advance. 提前致谢。

When faced with such doubts, and given that there are many bigint libraries out there, it is interesting to look into their code. 面对这样的疑虑,并且考虑到有很多bigint库,看看他们的代码是很有趣的。 I had a look at Java's BigInteger, which has a toString method, and they do two things: 我看了一下Java的BigInteger,它有一个toString方法,它们做了两件事:

Knuth, Donald, The Art of Computer Programming , Vol. Knuth,唐纳德, 计算机编程艺术 ,卷。 2, Answers to Exercises (4.4) Question 14. 2,练习答案(4.4)问题14。

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

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