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

        <small id='2bMu3'></small><noframes id='2bMu3'>

      1. <legend id='2bMu3'><style id='2bMu3'><dir id='2bMu3'><q id='2bMu3'></q></dir></style></legend><tfoot id='2bMu3'></tfoot>

        如何检测 32 位 int 上的整数溢出?

        How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
          <bdo id='dTX1K'></bdo><ul id='dTX1K'></ul>

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

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

              1. <legend id='dTX1K'><style id='dTX1K'><dir id='dTX1K'><q id='dTX1K'></q></dir></style></legend>
                1. 本文介绍了如何检测 32 位 int 上的整数溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道这样的话题被问了好几次,但我的问题是关于完整的 32 位 int 的溢出.例如:

                  I know such topic was asked several times, but my question is about overflow on full 32 bits of int. For example:

                    11111111111111111111111111111111 +
                    00000000000000000000000000000001 =
                    00000000000000000000000000000000   //overflow!
                  

                  我发现 topic 有类似的问题,但是算法并不完美.

                  I found topic with similar question about this, however the algorithm is not perfect.

                    11111111111111111111111111111111 +
                    00000000000000000000000000000000 =
                    00000000000000000000000000000000  //overflow!
                  

                  有没有什么简单、快速、安全的检查方法?

                  Is there any simple, fast, safer way to check this ?

                  推荐答案

                  Math.addExact 溢出时抛出异常

                  从 Java 8 开始,数学类:

                  Math.addExact throws exception on overflow

                  Since Java 8 there is a set of methods in the Math class:

                  • toIntExact(long)
                  • addExact(int,int)
                  • subtractExact(int,int)
                  • multiplyExact(int,int)

                  ……以及长期版本.

                  这些方法中的每一个都会抛出 ArithmeticException 如果发生溢出.否则,如果它在范围内,它们会返回正确的结果.

                  Each of these methods throws ArithmeticException if overflow happens. Otherwise they return the proper result if it fits within the range.

                  加法示例:

                  int x = 2_000_000_000;
                  int y = 1_000_000_000;
                  try {
                      int result = Math.addExact(x, y);
                      System.out.println("The proper result is " + result);
                  } catch(ArithmeticException e) {
                      System.out.println("Sorry, " + e);
                  }
                  

                  查看此在 IdeOne.com 上实时运行的代码.

                  对不起,java.lang.ArithmeticException:整数溢出

                  Sorry, java.lang.ArithmeticException: integer overflow

                  这篇关于如何检测 32 位 int 上的整数溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                2. <tfoot id='u5EeW'></tfoot>

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

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

                          <tbody id='u5EeW'></tbody>
                          <legend id='u5EeW'><style id='u5EeW'><dir id='u5EeW'><q id='u5EeW'></q></dir></style></legend>