2012-07-07 142 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