big int 编译器实现?

发布于 2024-07-23 12:00:15 字数 83 浏览 5 评论 0原文

我正在构建一个类似于 c 的编译器,但我希望它解析大于 2^32 的整数。 这怎么可能?大整数是如何在Python和Ruby之类的语言中实现的..!!

i am building a compiler similar to c , but i want it to parse integers bigger than 2^32 . hows it possible?how has been big integers been implemented in python and ruby like languages ..!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

柠栀 2024-07-30 12:00:15

有图书馆可以做这类事情。

查看 gmplib

There are libraries to do this sort of thing.

Check out gmplib.

暗地喜欢 2024-07-30 12:00:15

有很多大数字库,请参阅这篇维基百科文章以获取完整列表。

GMP(GNU 多精度算术库) 足以满足我遇到的一切。 NTL 大致相同,但是是面向对象的。

一般来说,如果您想推出自己的库,那么这些库用数组表示数字,其中数字的每个数字作为一个字符,但这需要大量工作。

There are lots of big number libraries, see this wikipedia article for a complete list.

GMP(GNU Multiple Precision Arithmetic Library) is sufficient for everything I have encountered. NTL is more of the same but is object orientated.

Generally these libraries represent the numbers with arrays with each digit of a number as a character if you want to roll your own but it is a lot of work.

彩虹直至黑白 2024-07-30 12:00:15

如果你想自己写,请跟随我的记忆之旅;-)。

过去,计算机使用 8 位。 我们经常需要计算大数字(例如> 255)。 我们都必须编写例程。 例如添加。

如果我们需要将两个字节的数字彼此相加,我们使用以下算法:

  • 添加最低有效字节。
  • 如果结果超过 8 位,则设置进位位。
  • 添加最高有效字节和进位标志(如果设置)。
  • 如果结果超过 8 位,则会产生溢出错误(但如果您想要超过 2 个字节,则无需执行此操作。

您可以将其扩展到更多字节/字/双字/q字以及其他运算符。

If you want to write it yourself, follow my trip through memory lane ;-).

In the old days, when computers used 8 bits. We often needed to calculate with big numbers (like > 255). And we all had to write the routines. For example the addition.

If we needed to add numbers of two bytes to each other we used the following algorithm:

  • Add the least significant bytes.
  • If the result exceeded 8 bits, the carry bit was set.
  • Add the most significant bytes and the carry flag (if set).
  • If the result exceeded 8 bits you produced an overflow error (but you don't need to do this if you want more that 2 bytes.

You can extend this to more bytes/words/dwords/qwords and to other operators.

紧拥背影 2024-07-30 12:00:15

我相信您将需要某种 bigint 库,这些库可以在网上找到,只需进行一些搜索,您可能会找到一个适合您的项目的库。

因为我相信,仅仅解析整数是不够的。 您的用户不仅希望存储这些数字,而且可能还希望对这些数字执行操作。

I believe you'll need some sort of bigint library, which are available on the net, just do a bit of searching and you may find one that's suitable for your project.

Because, simply parsing the integers, I believe, will not be enough. Your users will want not only to store, but also, probably, perform operation with such numbers.

梦回旧景 2024-07-30 12:00:15

Felix von Leitner 有一张幻灯片,涵盖了一些 bignum 基础知识。 我个人认为这是非常有信息性和技术性的。

There is a slide by Felix von Leitner that covers some bignum basics. Personally i think it is quite informative and technical.

∞琼窗梦回ˉ 2024-07-30 12:00:15

Matt McCutchen 的 C++ 大整数库

https://mattmccutchen.net/bigint/

仅 C++ 源代码。 使用起来非常简单。

C++ Big Integer Library from Matt McCutchen

https://mattmccutchen.net/bigint/

C++ source code only. Very simple to use.

与君绝 2024-07-30 12:00:15

你必须使用 c 中的某种结构来实现这一点。 如果您使用的是 x86 平台而不是 x64 平台,您会发现这很困难。 如果您使用的是 x86,请准备好熟悉汇编和进位标志。

祝你好运!

You would have to use some sort of struct in c to achieve this. You will find this is difficult if you are on and x86 platform and not x64 as well. If you're on x86, prepare to get very familiar with assembly and the carry flag.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文