看板 C_and_CPP 關於我們 聯絡資訊
程式結果不對, 去查資料才發現 unsigned char v1 = 0x80, v2 = 0x7f; unsigned int val1 = v1, val2 = ~v2; => val1 = 0x80 val2 = 0xffffff80 這樣看起來 ~v2 是先把 v2 提升到 int 後才做 ~: http://stackoverflow.com/a/3357684 在 ~ 計算前有 Integral promotions are performed. The type of result type is the type of the promoted operand. 而 &, |, ^ 則只是提升到較大的部份 The usual arithmetic conversion is per- formed. (5.3, 5.11, 5.12, 5.13, usual... 那個的 規則在 ch 5 一開始的部份) 而 Integral promotion 在 4.5, 所以 ~ 會先被 提升到 int... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.166.49.9
purincess:推一個! 05/11 14:40
purincess:所以如果要有(我猜的)預期效果要(int)(char)~v2 XD? 05/11 14:40
purincess:^(unsigned int)(unsigned char)不然會sign extension 05/11 14:41
suhorng:這樣其實也跟直接 & 0xff 把前面去掉差不多了w 05/11 14:41
purincess:對耶 XDDDDDDDD 05/11 14:56