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

      <tfoot id='qygM3'></tfoot>

        <bdo id='qygM3'></bdo><ul id='qygM3'></ul>
    2. <small id='qygM3'></small><noframes id='qygM3'>

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

      时间:2023-10-01
      <tfoot id='97jZ7'></tfoot><legend id='97jZ7'><style id='97jZ7'><dir id='97jZ7'><q id='97jZ7'></q></dir></style></legend>
        • <i id='97jZ7'><tr id='97jZ7'><dt id='97jZ7'><q id='97jZ7'><span id='97jZ7'><b id='97jZ7'><form id='97jZ7'><ins id='97jZ7'></ins><ul id='97jZ7'></ul><sub id='97jZ7'></sub></form><legend id='97jZ7'></legend><bdo id='97jZ7'><pre id='97jZ7'><center id='97jZ7'></center></pre></bdo></b><th id='97jZ7'></th></span></q></dt></tr></i><div id='97jZ7'><tfoot id='97jZ7'></tfoot><dl id='97jZ7'><fieldset id='97jZ7'></fieldset></dl></div>
            <tbody id='97jZ7'></tbody>

          <small id='97jZ7'></small><noframes id='97jZ7'>

              <bdo id='97jZ7'></bdo><ul id='97jZ7'></ul>
              • 本文介绍了两个整数相乘溢出导致负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                考虑一下 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.

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

                上一篇:按从低到高的顺序对整数进行排序 java 下一篇:负字符值 JAVA

                相关文章

                最新文章

              • <small id='7sNw0'></small><noframes id='7sNw0'>

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

                    <tfoot id='7sNw0'></tfoot>

                  1. <legend id='7sNw0'><style id='7sNw0'><dir id='7sNw0'><q id='7sNw0'></q></dir></style></legend>

                      <bdo id='7sNw0'></bdo><ul id='7sNw0'></ul>