1. 程式人生 > 其它 >【20221-06-24】JAVA學習日誌

【20221-06-24】JAVA學習日誌


今日進度:學習了>Java程式基礎>Java語言架構>整數運算,總進度【01|50】


【20221-06-24】JAVA學習日誌

日誌|釋出:2021-06-24 07:18:19 更新:2021-06-24 08:19:42

源站:https://www.cnblogs.com/seto/ Rss 訂閱 | 傳真 | 第【06】份,總 第 09 份.


Java的整數運算遵循四則運算規則,可以使用任意巢狀的小括號。四則運算規則和初等數學一致。例如:

// 四則運算

整數的數值表示不但是精確的,而且整數運算永遠是精確的,即使是除法也是精確的,因為兩個整數相除只能得到結果的整數部分:

int x = 12345 / 67; // 184

求餘運算使用%

int y = 12345 % 67; // 12345÷67的餘數是17

特別注意:整數的除法對於除數為0時執行時將報錯,但編譯不會報錯。

溢位

要特別注意,整數由於存在範圍限制,如果計算結果超出了範圍,就會產生溢位,而溢位不會出錯,卻會得到一個奇怪的結果:

// 運算溢位

要解釋上述結果,我們把整數214748364015換成二進位制做加法:

  0111 1111 1111 1111 1111 1111 1111 1000
+ 0000 0000 0000 0000 0000 0000 0000 1111
-----------------------------------------
  1000 0000 0000 0000 0000 0000 0000 0111

由於最高位計算結果為1

,因此,加法結果變成了一個負數。

要解決上面的問題,可以把int換成long型別,由於long可表示的整型範圍更大,所以結果就不會溢位:

long x = 2147483640;
long y = 15;
long sum = x + y;
System.out.println(sum); // 2147483655

還有一種簡寫的運算子,即+=-=*=/=,它們的使用方法如下:

n += 100; // 3409, 相當於 n = n + 100;
n -= 100; // 3309, 相當於 n = n - 100;

自增/自減

Java還提供了++運算和--運算,它們可以對一個整數進行加1和減1的操作:

// 自增/自減運算

Run

注意++寫在前面和後面計算結果是不同的,++n表示先加1再引用n,n++表示先引用n再加1。不建議把++運算混入到常規運算中,容易自己把自己搞懵了。

移位運算

在計算機中,整數總是以二進位制的形式表示。例如,int型別的整數7使用4位元組表示的二進位制如下:

00000000 0000000 0000000 00000111

可以對整數進行移位運算。對整數7左移1位將得到整數14,左移兩位將得到整數28

int n = 7;       // 00000000 00000000 00000000 00000111 = 7
int a = n << 1;  // 00000000 00000000 00000000 00001110 = 14
int b = n << 2;  // 00000000 00000000 00000000 00011100 = 28
int c = n << 28; // 01110000 00000000 00000000 00000000 = 1879048192
int d = n << 29; // 11100000 00000000 00000000 00000000 = -536870912

左移29位時,由於最高位變成1,因此結果變成了負數。

類似的,對整數28進行右移,結果如下:

int n = 7;       // 00000000 00000000 00000000 00000111 = 7
int a = n >> 1;  // 00000000 00000000 00000000 00000011 = 3
int b = n >> 2;  // 00000000 00000000 00000000 00000001 = 1
int c = n >> 3;  // 00000000 00000000 00000000 00000000 = 0

如果對一個負數進行右移,最高位的1不動,結果仍然是一個負數:

int n = -536870912;
int a = n >> 1;  // 11110000 00000000 00000000 00000000 = -268435456
int b = n >> 2;  // 11111000 00000000 00000000 00000000 = -134217728
int c = n >> 28; // 11111111 11111111 11111111 11111110 = -2
int d = n >> 29; // 11111111 11111111 11111111 11111111 = -1

還有一種無符號的右移運算,使用>>>,它的特點是不管符號位,右移後高位總是補0,因此,對一個負數進行>>>右移,它會變成正數,原因是最高位的1變成了0

int n = -536870912;
int a = n >>> 1;  // 01110000 00000000 00000000 00000000 = 1879048192
int b = n >>> 2;  // 00111000 00000000 00000000 00000000 = 939524096
int c = n >>> 29; // 00000000 00000000 00000000 00000111 = 7
int d = n >>> 31; // 00000000 00000000 00000000 00000001 = 1

byteshort型別進行移位時,會首先轉換為int再進行位移。

仔細觀察可發現,左移實際上就是不斷地×2,右移實際上就是不斷地÷2。

位運算

位運算是按位進行與、或、非和異或的運算。

與運算的規則是,必須兩個數同時為1,結果才為1

n = 0 & 0; // 0
n = 0 & 1; // 0
n = 1 & 0; // 0
n = 1 & 1; // 1

或運算的規則是,只要任意一個為1,結果就為1

n = 0 | 0; // 0
n = 0 | 1; // 1
n = 1 | 0; // 1
n = 1 | 1; // 1

非運算的規則是,01互換:

n = ~0; // 1
n = ~1; // 0

異或運算的規則是,如果兩個數不同,結果為1,否則為0

n = 0 ^ 0; // 0
n = 0 ^ 1; // 1
n = 1 ^ 0; // 1
n = 1 ^ 1; // 0

對兩個整數進行位運算,實際上就是按位對齊,然後依次對每一位進行運算。例如:

// 位運算

上述按位與運算實際上可以看作兩個整數表示的IP地址10.0.17.7710.0.17.0,通過與運算,可以快速判斷一個IP是否在給定的網段內。

運算優先順序

在Java的計算表示式中,運算優先順序從高到低依次是:

  • ()
  • ! ~ ++ --
  • * / %
  • + -
  • << >> >>>
  • &
  • |
  • += -= *= /=

記不住也沒關係,只需要加括號就可以保證運算的優先順序正確。

型別自動提升與強制轉型

在運算過程中,如果參與運算的兩個數型別不一致,那麼計算結果為較大型別的整型。例如,shortint計算,結果總是int,原因是short首先自動被轉型為int

// 型別自動提升與強制轉型

也可以將結果強制轉型,即將大範圍的整數轉型為小範圍的整數。強制轉型使用(型別),例如,將int強制轉型為short

int i = 12345;
short s = (short) i; // 12345

要注意,超出範圍的強制轉型會得到錯誤的結果,原因是轉型時,int的兩個高位位元組直接被扔掉,僅保留了低位的兩個位元組:

// 強制轉型

因此,強制轉型的結果很可能是錯的。

練習

計算前N個自然數的和可以根據公式:

\frac{(1+N)\times N}22(1+NN

請根據公式計算前N個自然數的和:

// 計算前N個自然數的和
public class Main {
    public static void main(String[] args) {
        int n = 100;
        System.out.println(sum);
        System.out.println(sum == 5050 ? "測試通過" : "測試失敗");
    }
}

小結

整數運算的結果永遠是精確的;

運算結果會自動提升;

可以強制轉型,但超出範圍的強制轉型會得到錯誤的結果;

應該選擇合適範圍的整型(intlong),沒有必要為了節省記憶體而使用byteshort進行整數運算。

----the END--------------------------------------------------------------------------------------------------------


Statement

Copyright of this article is the original author (s) and the original author (s).Content is the author's personal view and does not represent the station as endorsing its views and as accountable for its authenticity, and the sole reference to this station does not constitute any investment and application recommendations.This site is a platform for personal study communication, and part of the articles on the site are reprinted and not used for any commercial purpose, we have notified authors and sources as much as possible, but have limited capacity or negligence to cause missed publication, please contact us promptly, and we will correct or delete the content immediately upon request from the copyright holder.This station holds the final interpretation of this statement.
Statement: the article is reprinted, reproduced on board for the purpose of wider dissemination of marketing information, and does not represent an agreement with the company on its views.The contents of the article are for information only.