1. 資訊存儲 (Information Storage)
電腦最小的可定址單位是 Byte (8 bits)。
U8 = 0x00~0xFF
字組大小 (Word Size):32-bit 或 64-bit 決定了虛擬位址空間的最大範圍。
0x01234567在 Little Endian 記憶體中的排列為67 45 23 01- Big Endian通常小的在後(尾端)但是大部分cpu 都是使用 Little Endian
2. 整數表示法 (Integer Representations)
Two's Complement (二進位補數):用於表示正負數,最高位元(MSB)權重為負。
S8:
0000 0000 = 0
0100 0000 = 64
1000 0000 = -128
1100 0000 = -128+64 = -64
TMin 與 TMax 的不對稱性:在 8-bit 中,範圍是 -128 到 127。
3. 整數運算與溢位 (Integer Arithmetic)
整數運算的本質是 模運算 (Modular Arithmetic)。
Unsigned Addition:結果若超過 $2^w - 1$,則會減去 $2^w$(Wrap around)。
Two's Complement Addition:
正溢位:兩個正數相加得到負數。
負溢位:兩個負數相加得到正數。
乘法與位移:編譯器常用
shl(左移) 代替乘以 2 的冪次,用shr(右移) 代替除法。邏輯右移 (Logical):補 0(用於 Unsigned)。
算術右移 (Arithmetic):補符號位元(用於 Signed)。
4. 浮點數 (Floating Point)
1. 無符號整數溢位 (Unsigned Integer Overflow)
Unsigned Max: 4294967295
After Overflow (a + 1): 0
2. 有符號整數溢位 (Signed Integer Overflow)
Signed Max: 2147483647
After Overflow: -2147483648
3. 隱式型別轉換與正負號問題 (Implicit Casting & Signedness)
驚訝吧!-1 居然大於 1
原因:x 被轉成 unsigned 的數值是 4294967295