Bit computing
java has four bit operations, namely, "& bitwise and, bitwise or |, ^ bitwise exclusive OR, bitwise ~", their rules of operation are:
- Bitwise AND &: Two all 1, the result is 1
- Bitwise or |: there is a two to one, the result is 1
- Bitwise XOR ^: 0 of a two, a is 1, the result is a
- Bitwise ~ :( For signed numbers, including sign bit) = 0> = 1, 1> 0
For example: 2 & 3 = 2 2 | 3 = 3 2 ^ 3 = 1 ~ 2 = -3
Shift operation
java has three shift operator:
>>, and << Arithmetic shift right arithmetic shift left, arithmetic rules:
arithmetic shift right: low overflow, sign bits unchanged, with the sign bit and overflow high
arithmetic shift left: Symbol then keeping low fill 0
>>> Logical shift right arithmetic rules are: low overflow, high fill 0
比如,-1>>2=-1,1<<2=4,3>>>1=1
note
Computer operations are complement arithmetic