Preface:
The sigh time is always so short that it has been a long time since the previous series of articles were published. It has only been updated for the total reason, continue with our golang today ......
Operators in the go Language
The above table (derived from the network) clearly shows that the go operators have their priorities. For very common operators, we will not introduce them one by one. Here are some operators to play.
Let's take a look at the example: Question -- given an int-type array, find out the odd number and print it out ?. You can select any operator in the Table above to try it out.
This question is very simple. If a number cannot be divisible by 2, it is an odd number. Well, let's try the % operator. This operator is a modulo, that is, the remainder can be obtained, if the modulo of 2 is not 0, the result is an odd number. Check the Code:
An isodd method is defined here to determine whether the number is an odd number and the return value is of the bool type. A common error in this method is that some people mistakenly believe that the value after modulo 2 is equal to 1. If num % 2 = 1, the value is an odd number, But this is incorrect because when a negative number occurs, false is returned for both the odd and even numbers. If the remainder is a negative number, it is not equal to 1 in any case. If you compile the above Code, something strange will happen. Nima is actually a virus ??? Although it is a false positive, I am still surprised. I don't know how it works on your machine.
In fact, there is no need to think that the anti-virus software generally judges whether it is a virus based on the signature. Therefore, it is very likely that the application compiled by a program is exactly the same, therefore, we often see that anti-virus software has mistakenly killed the software. To solve this problem, it is actually easy. Sometimes you only need to adjust the code structure or content. Here I add a line feed code FMT. println () under the printed result, and it will be okay.
In addition to the % operator, you can also use the & operator to determine the odd number. The principle is:
(1) When bitwise operations are performed, we know that any odd binary number indicates that the bitwise is 1,
For example, the binary 0000 0001 of 1 (which is represented by 8-bit binary here for convenience of description), the binary 0000 0011 of 3, and the binary 0001 0101 of 21...
(2) and the operation rule is 1 & 1 = 1, 0 & 0 = & 1 = & 0 = 0. in short, 0 & any number is 0. Only 1 & 1 is 1.
Then, based on (1) (2), we can calculate the number of a binary value of 0000 and any number. If the number of a phase with 1 is an odd number, the result is 0 at the highest position, and the first digit 1 & 1 = 1 is 0000 0001.
The final code is as follows:
Let's take a look at the Special operators in the go language.
Suppose a & ^ B, then this operation will clear all bits of B from A. What does this mean? Let's look at the example:
A: 0000 0000 0010 0011 = 35
B: 0000 0000 1100 0101 = 197
==============================
0000 0000 0010 0010 = 34
A flag is a bit of 1. If you think it is difficult to remember this, it is actually a ^ B (XOR) operation first, then perform the & Operation on the result and B.
You can verify the result through a program:
This is the time to come. The usage of more operators will be detailed later.