我曾经使用 >> 操作符进行右移操作。现在我已经将其替换为 >>>,并发现得到了相同的结果。因此,我无法确定这两者是否本质上相等。
第一个运算符使用符号扩展值,将符号位进行复制移位;第二个运算符始终移位为零。
这样做的原因是为了模拟无符号整数进行位操作,部分弥补Java中缺少无符号整数类型的不足。
这个链接解释得非常好。在同一页上还有一个简短的示例。
但是为了一个真正简短的摘要:
<< 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