2014年3月27日

& 0xFF的意思



& 0xFF的意思


只取後面8bit的意思

int 的長度是 4Bytes, & 是 and 運算子,就是將
每個位元拿來進行 and 運算,根據 and 運算的規則
只有兩者皆為 true (1) 的結果才為 1

假設這個 i 是 12345 => 0x3039 (Hex)

i & 0xFF

0x3039=>0011000000111001(Bin)
0xFF =>0000000011111111
____________________________
結果:  0000000000111001

由於前面的 bits 都是 0 ,所以實際上有用的只有後面的 8 bits



Reference:

http://acm.nudt.edu.cn/~twcourse/BitwiseOperation.html

http://programming.im.ncnu.edu.tw/Chapter5.htm