java 大数字经常变成负数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17218964/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:20:02  来源:igfitidea点击:

Often big numbers become negative

javabigintegerinteger-overflow

提问by user2435678

Since I started using eclipse for project euler, I noticed that big numbers sometime become a seemingly random negative numbers. I suppose this has something to do with passing the boudry of the type.

自从我开始将 eclipse 用于项目 euler 以来,我注意到大数有时会变成看似随机的负数。我想这与传递类型的边界有关。

I'll be glad if you could explain to me how these negative numbers are generated and what is the logic behind it. Also, how can I avoid them (preferable not with BigInteger class). Danke!=)

如果您能向我解释这些负数是如何生成的以及背后的逻辑是什么,我会很高兴。另外,我该如何避免它们(最好不要使用 BigInteger 类)。丹克!=)

回答by Goatcat

This image shows what you're looking for. In your case it's obviously larger numbers, but the principle stays the same.

Examples of limits in java are:
int: ?2,147,483,648 to 2,147,483,647.
long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

此图像显示了您要查找的内容。在您的情况下,它显然是更大的数字,但原则保持不变。

java 中的限制示例有:
int: ?2,147,483,648 到 2,147,483,647。
长:-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807


In the image 0000, 0001 etc, shows the binary representation of the numbers.


在图像 0000、0001 等中,显示了数字的二进制表示。

Image explaining two's complement

解释二进制补码的图像

EDIT: In project euler you often have to think of a way to work around the lagre numbers. The problems are designed with numbers that big so that you can't use the ordinary way of problem solving. However, if you find that you really need to use them, i suggest studying BigInteger anyway. You will find it useful in the long run, and it's not all that complicated. Here is a link with lots of understandable examples: BigInteger Example

编辑:在项目 euler 中,您经常需要想办法解决大数。问题是用这么大的数字设计的,所以你不能用普通的方法解决问题。然而,如果你发现你真的需要使用它们,我建议无论如何学习 BigInteger。从长远来看,您会发现它很有用,而且并没有那么复杂。这是一个包含许多可理解示例的链接: BigInteger Example

回答by Elazar

Here's a two's complementrepresentation for 2-bit integer: (U means Unsigned, S means Signed)

这是2 位整数的二进制补码表示:(U 表示无符号,S 表示有符号)

 U | bits |  S
---------------
 0 |  00  |  0 
 1 |  01  |  1 \ overflow here:
 2 |  10  | -2 /   1 + 1 = -2
 3 |  11  | -1

Arithmetic is done mostly like in the unsigned case, modulo max(U) (4 in our case).

算术主要是在无符号情况下完成的,模 max(U)(在我们的例子中是 4)。

The logic is the same for bigger types. intin Java is 32 bit. Use longfor 64 bits.

对于更大的类型,逻辑是相同的。int在 Java 中是 32 位。使用long64位。

回答by AlexR

In mathematics numbers are infinite. However in computers they are not. There is MAX_VALUEfor each int-like type: int, short, long. For example Integer.MAX_VALUE. When you try to increase number more than this value the number becomes negative. This way the internal binary representation of numbers work.

在数学中,数字是无限的。但是在计算机中它们不是。有MAX_VALUE每个int样类型:intshortlong。例如Integer.MAX_VALUE。当您尝试将数字增加到超过此值时,数字变为负数。这样数字的内部二进制表示就起作用了。

int i = Integer.MAX_VALUE;
i++; // i becomes negative. 

回答by wlyles

You are probably overflowing the size of your data type, since the most significant bit is the sign bit. I don't think that Java has unsigneddata types, so you may try using a larger data type such as longif you want to hold bigger numbers than int. If you are still overflowing a longthough, you're pretty much stuck with BigInteger.

您可能会溢出数据类型的大小,因为最高有效位是符号位。我认为 Java 没有unsigned数据类型,因此您可以尝试使用更大的数据类型,例如long如果您想保存比int. 如果您仍然溢出 a long,那么您几乎无法使用BigInteger.