This is a creation in Article, where the information may have evolved or changed.
Http://blog.opskumu.com/golang-bitwise.html
Bit operation is a unary and two-dollar operation of the bitwise or binary number of the bit-mode in the program design. On many ancient microprocessors, bit operations are slightly faster than addition and subtraction, and the bitwise operations are much faster than the multiplication algorithm. In modern architectures, this is not the case: bit operations are usually the same as the addition operation (still faster than multiplication). Bitwise operators
First, Introduction
On the operation of bitwise operations, Wikipedia's theoretical introduction of bitwise operators is very clear. About Golang bit operation is similar to the C language, in addition to reverse operation and C language a little different, Golang use ^x
, and C is to use ~x
reverse.
Second, the concept
<<
[Move left]
- 1 << 2 = = 4
- Output 0100, more common than the right shift, the rest of the post-shift blank 0
>>
[Move right]
x ^ y
[XOR]
- 10 ^ 2 = = 8
- The result of the operation is that if a bit is different then the bit is 1, otherwise the bit is 0
x | y
Or
- 10 | 2 = = 10
- As long as one of the two corresponding binaries is 1, the result value of the bit is 1
x & y
and
- & 2 = = 2
- Two corresponding binary is 1, the result value of this bit is 1, otherwise 0
^x
[Take the counter]
- ^2 = =-3
- Minus 1, reverse complement.
Third, reference
- bit operator
- golang Bit operation details