<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          StackOverflow熱帖:Java整數(shù)相加溢出怎么辦?

          共 2021字,需瀏覽 5分鐘

           ·

          2020-11-07 04:21

          點擊上方藍(lán)色“程序猿DD”,選擇“設(shè)為星標(biāo)”

          回復(fù)“資源”獲取獨家整理的學(xué)習(xí)資料!

          作者 |?Aaron_濤

          來源 |?blog.csdn.net/qq_33330687/article/details/81626157

          # 問題


          在之前刷題的時候遇見一個問題,需要解決int相加后怎么判斷是否溢出,如果溢出就返回Integer.MAX_VALUE


          # 解決方案


          JDK8已經(jīng)幫我們實現(xiàn)了Math下,不得不說這個方法是在StackOverflow找到了的,確實比國內(nèi)一些論壇好多了~


          加法

          public static int addExact(int x, int y) {        int r = x + y;        // HD 2-12 Overflow iff both arguments have the opposite sign of the result        if (((x ^ r) & (y ^ r)) < 0) {            throw new ArithmeticException("integer overflow");        }        return r;}

          減法

           public static int subtractExact(int x, int y) {        int r = x - y;        // HD 2-12 Overflow iff the arguments have different signs and        // the sign of the result is different than the sign of x        if (((x ^ y) & (x ^ r)) < 0) {            throw new ArithmeticException("integer overflow");        }        return r;}

          乘法

          public static int multiplyExact(int x, int y) {        long r = (long)x * (long)y;        if ((int)r != r) {            throw new ArithmeticException("integer overflow");        }        return (int)r;}

          注意 long和int是不一樣的

            public static long multiplyExact(long x, long y) {        long r = x * y;        long ax = Math.abs(x);        long ay = Math.abs(y);        if (((ax | ay) >>> 31 != 0)) {            // Some bits greater than 2^31 that might cause overflow            // Check the result using the divide operator            // and check for the special case of Long.MIN_VALUE * -1           if (((y != 0) && (r / y != x)) ||               (x == Long.MIN_VALUE && y == -1)) {                throw new ArithmeticException("long overflow");            }        }        return r;}

          如何使用?


          直接調(diào)用是最方便的,但是為了追求速度,應(yīng)該修改一下,理解判斷思路,因為異常是十分耗時的操作,無腦異常有可能超時


          # 寫這個的目的


          總結(jié)一下,也方便告訴他人Java幫我們寫好了函數(shù)。



          往期推薦

          星巴克是如何處理訂單的?

          Redis在Linux系統(tǒng)的配置優(yōu)化

          MIT黑科技:通過手機(jī)記錄的咳嗽數(shù)據(jù)檢測是否感染新冠病毒

          為什么我使用了索引,查詢還是慢?

          10個你可能不曾用過卻很有用的 LINUX 命令

          分享一個Java開發(fā)都用得到的密碼摘要算法包



          掃一掃,關(guān)注我

          一起學(xué)習(xí),一起進(jìn)步

          每周贈書,福利不斷


          深度內(nèi)容

          推薦加入


          最近熱門內(nèi)容回顧? ?#技術(shù)人系列

          瀏覽 24
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  青娱乐成人在线观看视频 | 亚洲人妻A V | 蜜 桃 黄 片AV在线观看 91人妻人人澡人人爽人人精品 | 伊人日逼 | 一区二区无码高清 |