久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      • <bdo id='NSYFO'></bdo><ul id='NSYFO'></ul>
      1. <i id='NSYFO'><tr id='NSYFO'><dt id='NSYFO'><q id='NSYFO'><span id='NSYFO'><b id='NSYFO'><form id='NSYFO'><ins id='NSYFO'></ins><ul id='NSYFO'></ul><sub id='NSYFO'></sub></form><legend id='NSYFO'></legend><bdo id='NSYFO'><pre id='NSYFO'><center id='NSYFO'></center></pre></bdo></b><th id='NSYFO'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='NSYFO'><tfoot id='NSYFO'></tfoot><dl id='NSYFO'><fieldset id='NSYFO'></fieldset></dl></div>

        <legend id='NSYFO'><style id='NSYFO'><dir id='NSYFO'><q id='NSYFO'></q></dir></style></legend>

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

        <tfoot id='NSYFO'></tfoot>

        Java 如何處理整數下溢和上溢以及如何檢查它?

        How does Java handle integer underflows and overflows and how would you check for it?(Java 如何處理整數下溢和上溢以及如何檢查它?)
          <legend id='LV9h8'><style id='LV9h8'><dir id='LV9h8'><q id='LV9h8'></q></dir></style></legend>
          <tfoot id='LV9h8'></tfoot>
            <tbody id='LV9h8'></tbody>

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

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

                1. 本文介紹了Java 如何處理整數下溢和上溢以及如何檢查它?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  Java 如何處理整數下溢和上溢?

                  How does Java handle integer underflows and overflows?

                  在此基礎上,您將如何檢查/測試這種情況是否正在發生?

                  Leading on from that, how would you check/test that this is occurring?

                  推薦答案

                  如果溢出,返回到最小值 并從那里繼續.如果下溢,則返回 最大值 并從那里繼續.

                  If it overflows, it goes back to the minimum value and continues from there. If it underflows, it goes back to the maximum value and continues from there.

                  您可以按如下方式預先檢查:

                  You can check that beforehand as follows:

                  public static boolean willAdditionOverflow(int left, int right) {
                      if (right < 0 && right != Integer.MIN_VALUE) {
                          return willSubtractionOverflow(left, -right);
                      } else {
                          return (~(left ^ right) & (left ^ (left + right))) < 0;
                      }
                  }
                  
                  public static boolean willSubtractionOverflow(int left, int right) {
                      if (right < 0) {
                          return willAdditionOverflow(left, -right);
                      } else {
                          return ((left ^ right) & (left ^ (left - right))) < 0;
                      }
                  }
                  

                  (您可以將 int 替換為 long 以對 long 執行相同的檢查)

                  (you can substitute int by long to perform the same checks for long)

                  如果您認為這種情況可能經常發生,請考慮使用可以存儲更大值的數據類型或對象,例如long 或者 java.math.BigInteger.最后一個不溢出,實際可用的JVM內存就是極限了.

                  If you think that this may occur more than often, then consider using a datatype or object which can store larger values, e.g. long or maybe java.math.BigInteger. The last one doesn't overflow, practically, the available JVM memory is the limit.

                  如果您碰巧已經在使用 Java8,那么您可以使用新的 Math#addExact()Math#subtractExact() 方法會拋出 ArithmeticException 溢出.

                  If you happen to be on Java8 already, then you can make use of the new Math#addExact() and Math#subtractExact() methods which will throw an ArithmeticException on overflow.

                  public static boolean willAdditionOverflow(int left, int right) {
                      try {
                          Math.addExact(left, right);
                          return false;
                      } catch (ArithmeticException e) {
                          return true;
                      }
                  }
                  
                  public static boolean willSubtractionOverflow(int left, int right) {
                      try {
                          Math.subtractExact(left, right);
                          return false;
                      } catch (ArithmeticException e) {
                          return true;
                      }
                  }
                  

                  源碼可以找到這里 和 這里分別.

                  The source code can be found here and here respectively.

                  當然,您也可以直接使用它們,而不是將它們隱藏在 boolean 實用程序方法中.

                  Of course, you could also just use them right away instead of hiding them in a boolean utility method.

                  這篇關于Java 如何處理整數下溢和上溢以及如何檢查它?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  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?)
                2. <legend id='QUAUz'><style id='QUAUz'><dir id='QUAUz'><q id='QUAUz'></q></dir></style></legend>

                  <tfoot id='QUAUz'></tfoot>
                    <tbody id='QUAUz'></tbody>
                      <bdo id='QUAUz'></bdo><ul id='QUAUz'></ul>

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

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

                          • 主站蜘蛛池模板: 国产成人精品午夜视频免费 | 一区二区三区四区在线视频 | 色在线免费视频 | 中文字幕99 | 天堂一区| 精品亚洲一区二区三区四区五区 | 久久久久久久久91 | 国产精品毛片 | 国产日韩欧美中文 | 精品久久一区 | 国产乱码久久久久久一区二区 | 亚洲精品日韩精品 | 国产精品成人一区二区三区 | 美女午夜影院 | 久久久精品 | 午夜精品久久久久久久99黑人 | 亚洲午夜精品一区二区三区 | 日韩av一区二区在线观看 | 欧美性区 | 青春草国产 | 国产精品欧美大片 | 精品国产乱码久久久久久蜜柚 | 成人一区二区三区在线观看 | 亚洲国产aⅴ精品一区二区 免费观看av | 粉嫩一区二区三区国产精品 | 色综合99 | 成人午夜免费福利视频 | 黄色一级电影在线观看 | 欧美一区免费 | 日本久久www成人免 成人久久久久 | 毛片高清| 久久久视| 国产精品久久久久久久久久三级 | 色网站入口 | 国产乱码精品1区2区3区 | 91精品中文字幕一区二区三区 | 欧美大片一区二区 | 91成人在线视频 | 亚洲 欧美 综合 | 毛片电影| 久久一区二区三区四区五区 |