Often big numbers become negative

后端 未结 4 1815
面向向阳花
面向向阳花 2020-11-28 15:28

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 pass

4条回答
  •  佛祖请我去吃肉
    2020-11-28 15:53

    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


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

    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

提交回复
热议问题