1. 程式人生 > >java int溢位,結果只會保留低32位,高位會拋棄掉

java int溢位,結果只會保留低32位,高位會拋棄掉

今天做leetcode題目時,發現int mid = 536848900,但是mid*mid=484528144,我說為啥程式執行一直出錯呢!!

     int mid = 536848900;
        System.out.println(mid * mid);//484528144

        long mid2 = 536848900;//long * long
        System.out.println(mid2 * mid2);//288206741431210000

        System.out.println(Integer.MAX_VALUE);//2147483647
        System.out.println(Math.pow(2, 31) - 1);//2.147483647E9

        int a = Integer.MAX_VALUE;
//        a += 1;//-2147483648
        a += 100;//-2147483549

        long MonthNanoSeconds1 = 30 * 24 * 3600 * 1000 * 1000;//右邊的結果是int,已溢位

        //int * long,與最後一個long型的1000相乘之前就已經溢位,所以結果也不對
        long MonthNanoSeconds2 = 30 * 24 * 3600 * 1000 * 1000L;

        //這是正確的寫法
        long MonthNanoSeconds3 = 30L * 24 * 3600 * 1000 * 1000;//long * int

        System.out.println(MonthNanoSeconds1);//2134720512
        System.out.println(MonthNanoSeconds2);//-1702967296000
        System.out.println(MonthNanoSeconds3);//2592000000000
        System.out.println(30 * 24 * 3600 * 1000);//-1702967296
        System.out.println(30 * 24 * 3600 * 1000 * 1000);//2134720512

在Java Language Specifictionz中所述(JSL 15.7.1)

If an integer multiplication overflows, then the result is the low-order bits of the mathematical product as represented in some sufficiently large two’s-complement format. As a result, if overflow occurs, then the sign of the result may not be the same as the sign of the mathematical product of the two operand values.

也就是說int型整數相乘,結果只會保留低32位,高位會拋棄掉。

加減乘除的運算子優先順序都是從左向右。

參考:

《java int溢位總結》 https://njucz.github.io/2017/08/16/java-int%E6%BA%A2%E5%87%BA%E6%80%BB%E7%BB%93/ 
《java中short、int、long、float、double取值範圍》 https://blog.csdn.net/qfikh/article/details/52832087 
《[Java]運算子優先順序參考圖表》 https://blog.csdn.net/xiaoli_feng/article/details/4567184