This is a creation in Article, where the information may have evolved or changed.
http://blog.guoqiangti.com/?p=538
The binary was rarely manipulated before, and it seemed as if you didn't see a description of the binary operator when looking at effective go. Today in the look at other source code, see some binary operators, do not know what he means, so look up information, review.
- Bitwise OR |
.| (bitwise OR): "Jiayi" makes a bitwise logical OR operation on the binary form of two integers, the principle is: 1|0=1,0|0=0,1|1=1,0|1=1
such as
4|-5:
The 4 binary form is: 00000000 00000000 00000000 00000100,
-5 binary form: 11111111 11111111 11111111 11111011,
Bitwise logic OR operation: 11111111 11111111 11111111 11111111, that is, get-1.
2. & (Bitwise AND)
"Jiayi" The binary form of two integers is bitwise logical AND operation, principle: 1|0=0,0|0=0,1&1=1;0&1=0 and so on.
4&-5:
The 4 binary form is: 00000000 00000000 00000000 00000100,
-5 binary form: 11111111 11111111 11111111 11111011,
Bitwise logic AND operation: 00000000 00000000 00000000 00000000, that is, the resulting 0.
Practical application: Can convert bytes to integers, -64&0xff=192, can also be in the form of octal, -64&0377=192,
In fact, both 0xFF and 0377 represent integers 255,
3.^ (Bitwise XOR)
"Jiayi" The binary form of two integers is bitwise logical XOR operation, principle: 1^1=0,1^0=1,0^1=1,0^0=0.
4^-5:
The 4 binary form is: 00000000 00000000 00000000 00000100,
-5 binary form: 11111111 11111111 11111111 11111011,
Bitwise logical XOR Operation: 11111111 11111111 11111111 11111111, i.e. get-1.
Practical application: Bitwise XOR can compare two numbers equal, it uses the principle of 1^1=0,0^0=0. 20^20==0