• <legend id='GmxAc'><style id='GmxAc'><dir id='GmxAc'><q id='GmxAc'></q></dir></style></legend>

        <tfoot id='GmxAc'></tfoot>

        <i id='GmxAc'><tr id='GmxAc'><dt id='GmxAc'><q id='GmxAc'><span id='GmxAc'><b id='GmxAc'><form id='GmxAc'><ins id='GmxAc'></ins><ul id='GmxAc'></ul><sub id='GmxAc'></sub></form><legend id='GmxAc'></legend><bdo id='GmxAc'><pre id='GmxAc'><center id='GmxAc'></center></pre></bdo></b><th id='GmxAc'></th></span></q></dt></tr></i><div id='GmxAc'><tfoot id='GmxAc'></tfoot><dl id='GmxAc'><fieldset id='GmxAc'></fieldset></dl></div>
        • <bdo id='GmxAc'></bdo><ul id='GmxAc'></ul>

        <small id='GmxAc'></small><noframes id='GmxAc'>

        两个整数相乘溢出导致负数

        Multiplication of two ints overflowing to result in a negative number(两个整数相乘溢出导致负数)
        <legend id='zrCVa'><style id='zrCVa'><dir id='zrCVa'><q id='zrCVa'></q></dir></style></legend>
      1. <tfoot id='zrCVa'></tfoot>
        • <bdo id='zrCVa'></bdo><ul id='zrCVa'></ul>

          <i id='zrCVa'><tr id='zrCVa'><dt id='zrCVa'><q id='zrCVa'><span id='zrCVa'><b id='zrCVa'><form id='zrCVa'><ins id='zrCVa'></ins><ul id='zrCVa'></ul><sub id='zrCVa'></sub></form><legend id='zrCVa'></legend><bdo id='zrCVa'><pre id='zrCVa'><center id='zrCVa'></center></pre></bdo></b><th id='zrCVa'></th></span></q></dt></tr></i><div id='zrCVa'><tfoot id='zrCVa'></tfoot><dl id='zrCVa'><fieldset id='zrCVa'></fieldset></dl></div>

            <small id='zrCVa'></small><noframes id='zrCVa'>

              <tbody id='zrCVa'></tbody>

                  本文介绍了两个整数相乘溢出导致负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑一下 Java 语言规范中的这段代码.

                  Consider this snippet from the Java language specification.

                  class Test {
                      public static void main(String[] args) {
                          int i = 1000000;
                          System.out.println(i * i);
                          long l = i;
                          System.out.println(l * l);
                      }
                  }
                  

                  输出是

                  -727379968
                  1000000000000
                  

                  为什么 (i*i) 的结果是 -727379968?理想情况下应该是 1000000000000.

                  Why is the result -727379968 for (i*i)? Ideally it should be 1000000000000.

                  我知道 Integer 的范围是从 –2147483648 到 2147483647.所以显然 1000000000000不在给定范围内.

                  I know the range of Integer is from –2147483648 to 2147483647. so obviously 1000000000000 is not in the given range.

                  为什么结果会变成-727379968?

                  推荐答案

                  Java(就像现在的大多数计算机架构一样)使用一种叫做 二进制补码算法,它使用整数的最高有效位来表示一个数字是否为负数.如果你将两个大数相乘,你会得到一个大到设置最高位的数,结果是负数.

                  Java (like most computer architectures these days) uses something called two's complement arithmetic, which uses the most significant bit of an integer to signify that a number is negative. If you multiply two big numbers, you end up with a number that's so big it sets that highest bit, and the result ends up negative.

                  这篇关于两个整数相乘溢出导致负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)
                  <tfoot id='Qv40p'></tfoot>

                  • <i id='Qv40p'><tr id='Qv40p'><dt id='Qv40p'><q id='Qv40p'><span id='Qv40p'><b id='Qv40p'><form id='Qv40p'><ins id='Qv40p'></ins><ul id='Qv40p'></ul><sub id='Qv40p'></sub></form><legend id='Qv40p'></legend><bdo id='Qv40p'><pre id='Qv40p'><center id='Qv40p'></center></pre></bdo></b><th id='Qv40p'></th></span></q></dt></tr></i><div id='Qv40p'><tfoot id='Qv40p'></tfoot><dl id='Qv40p'><fieldset id='Qv40p'></fieldset></dl></div>
                      <tbody id='Qv40p'></tbody>
                    <legend id='Qv40p'><style id='Qv40p'><dir id='Qv40p'><q id='Qv40p'></q></dir></style></legend>

                          • <bdo id='Qv40p'></bdo><ul id='Qv40p'></ul>
                          • <small id='Qv40p'></small><noframes id='Qv40p'>