如何在 Java 或 C++ 中表示 128 位整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3132222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I represent a 128bit integer in Java or C++?
提问by dato datuashvili
Is it possible to have a 128bit integer in Java or C++?
是否可以在 Java 或 C++ 中使用 128 位整数?
回答by MAK
In Java, you can use the BigIntegerclass to store arbitrarily large integers. In C++ you can use a library like GMPto get the same functionality.
在 Java 中,您可以使用BigInteger类来存储任意大的整数。在 C++ 中,您可以使用像GMP这样的库来获得相同的功能。
回答by Prav
Write your own class and operations for representing 128 bit numbers or use some library available.
编写您自己的类和操作来表示 128 位数字或使用一些可用的库。
回答by Andreas Dolk
The BigIntegerclass is designed for integer values bigger then Long.MAX_VALUE.
该BigInteger班是专为再整数值更大Long.MAX_VALUE。
回答by samoz
You can. You will most likely need to use a library to do this though, at least for C++.
你可以。不过,您很可能需要使用库来执行此操作,至少对于 C++ 而言。
I like the PolarSSLlibrary or the GNU MP Bignumlibrary.
我喜欢PolarSSL库或GNU MP Bignum库。
回答by Joachim Sauer
Of course you can represent them.
你当然可以代表他们。
At leastyou can use a byte-array with 16 elements.
在至少你可以使用一个字节数组有16个元素。
However, the question is if you just want to representthe value or actually do some calculationswith it.
但是,问题是您是否只想表示该值或实际使用它进行一些计算。
In Java you can use BigIntegerto represent (effectively) arbitrary sized integer values anddo calculations as well.
在 Java 中,您可以使用BigInteger(有效地)表示任意大小的整数值并进行计算。
回答by Emperorlou
java.math.BigInteger
java.math.BigInteger
To work with integers that are larger than 64 bits (the size of a long), use java.math.BigInteger. This class represents unbounded integers and provides a number of methods for doing arithmetic with them.
要处理大于 64 位(long 的大小)的整数,请使用 java.math.BigInteger。此类表示无界整数,并提供了许多对它们进行算术运算的方法。
http://leepoint.net/notes-java/data/numbers/10biginteger.html
http://leepoint.net/notes-java/data/numbers/10biginteger.html
If you need decimal values use BigDecimal
如果您需要十进制值,请使用BigDecimal
回答by Venkat
Of course, you can use BigIntegerclass in java.math package. This class provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation like operations.
当然,您可以使用java.math 包中的BigInteger类。此类提供模算术、GCD 计算、素性测试、素数生成、位操作等操作。
This class has been added in JDK1.1itself.
这个类已经在JDK1.1本身中添加了。
But I don't know there is such a availability built into with C++ library. There can be a extensible API from third parties.
但我不知道 C++ 库中内置了这样的可用性。可以有来自第三方的可扩展 API。

