Golang binary operation

Source: Internet
Author: User
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.

    1. 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.