2012-07-07 141 views

回答

6

第一個運算符符號擴展該值,移入符號位的副本;第二個總是轉變爲零。

其原因是爲了進行位操作而模仿無符號整數,部分彌補了Java中無符號整型的缺失。

11

>>是右移算術(有符號),>>>是邏輯(無符號)右移,如Java tutorial中所述。試試他們的負值,你會看到一個區別。

3

This explains it really well。在同一頁上還有一個簡短的example

但對於一個真正的簡短摘要:

<< signed left shift - shifts a bit pattern to the left 
    0 0 1 1 1 => 0 1 1 1 0 

>> signed right shift - shifts a bit pattern to the right 
    0 0 1 1 1 => 0 0 0 1 1 

>>> unsigned right shift - shifts a zero into the leftmost position 
    1 1 1 0 => 0 0 1 1 

~ unary bitwise complement operator 
    A | Result 
    0 | 1 
    1 | 0 
    0 | 1 
    1 | 0 

& bitwise and 
    A | B | Result 
    0 | 0 | 0 
    1 | 0 | 0 
    0 | 1 | 0 
    1 | 1 | 1 

^ xor 
    A | B | Result 
    0 | 0 | 0 
    1 | 0 | 1 
    0 | 1 | 1 
    1 | 1 | 0 

| inclusive or 
    A | B | Result 
    0 | 0 | 0 
    1 | 0 | 1 
    0 | 1 | 1 
    1 | 1 | 1