[心得] bitwise operator 的轉型
程式結果不對, 去查資料才發現
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
→
05/11 14:40, , 1F
05/11 14:40, 1F
→
05/11 14:40, , 2F
05/11 14:40, 2F
→
05/11 14:41, , 3F
05/11 14:41, 3F
→
05/11 14:41, , 4F
05/11 14:41, 4F
→
05/11 14:56, , 5F
05/11 14:56, 5F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章