& 位运算 AND| 位运算 OR^ 位运算 XOR&^ 位清空 (AND NOT)<< 左移>> 右移
Although the sense bit operator is not used much at ordinary times, it is more interesting when it comes to underlying performance optimizations or the use of certain trick.
& (And) | (OR) Do not mention that the most commonly used things will be programmed.
^ (XOR) in the Go language, XOR is present as a two-dollar operator:
But if it appears as a unary operator, he means to take a bitwise inverse, for example
" FMT " Func Main () { x:4 FMT. Println (^x)}
Output: 5
If the operator is a two-dollar
" FMT " Func Main () { x:4 y:2 FMT. Println (x^y)}
Output:6
XOR is not a carry addition calculation, that is, XOR or computation. 0000 0100 + 0000 0010 = 0000 0110 = 6
The &^ (and not) bit emptying operation is related to the position of the operator variable, first look at an example:
" FMT " Func Main () { x:2 y:4 FMT. PRINTLN (x&^y)}
Output:2
Calculate X&^y First we convert to 2 binary 0000 0010 &^ 0000 0100 = 0000 0010 If the number on the Ybit bit is 0, take the value of the corresponding position on X, if the ybit bit is 1 then take the result bit 0
>> right shift << left shift feel right shift left should also be very common using the continue to see example:
" FMT " Func Main () { x:2 y:4 FMT. PRINTLN (x<<1) FMT. Println (y>>1)}
Output:4 2
Convert to binary and move left or right.
The use of the bitwise operator of the Golang