Go core development study notes (seven) - Operator

Operators: used to represent data operations, assignment and comparison

  1. Arithmetic operators: + - * /% + - negative, etc.
  2. Assignment operator + = = - = = * / =% =
  3. Comparison (relationship) operator> = <= <> ==! =
  4. The logical operators && ||!
  5. Bitwise Operators
  6. Other operators & address-value *

Expressions concept: anything of value can be seen as an expression

Operator precedence:

  1. Operators have different priorities, i.e. the order of operations.
  2. Only unary operators, the assignment operator is right-to-left.
  3. Highest to lowest priority: a suffix (in parentheses, +, -, etc.), a monocular (! ± negative, ~, * & address-value), arithmetic operators, shift operators, comparison operators, Bitwise operators, logical operators, assignment operators, comma

Arithmetic operators
see examples and comments

package main
import "fmt"
func main() {

//默认除数结果带小数,必须被除数要为小数10.0
fmt.Println(10 / 4)
fmt.Println(10.0 / 4)

//取模公式:无所谓包含负数或者不包含,a%b = a- a/b*b
fmt.Println(10 % 3)
fmt.Println(-10 % 3)
fmt.Println(10 % -3)

//自增自减
var i int = 10
i++
fmt.Println(i)      //11
i--

fmt.Println(i)      //10
}
  1. In addition to integer and fractional addition to differences, do integer division will remove the fractional part.
  2. Modulo nature: a% b = a- (a / b) * b.
  3. Increment decrement only when the independent language, such as i ++, i-, but not a: = i ++ (i-), can not be compared i ++> 0 too.
  4. ★★★ increment and decrement ++ i -i not the only i ++ and i-, and praising this!
  5. Golang easily removed increment decrement confusingly written, the golang more concise, unified (mandatory).

Assignment Operators

  1. C = A + B A + B is the result of imparting C
    C = A + C = C + A is
    so

Ary

  1. Golang does not support binary representation of a number, a binary number to be output if the number is used fmt.Printf ( "% b", i) manner
  2. Octal can support var i int = 011 this representation
  3. Hexadecimal support var i int = 0x11 indicates that

The original code, anti-code complement

  1. Signed numbers, the sign bit is a binary high, positive 1 0 negative
  2. Positive original code, anti-code, are the same complement => 1 Original: [0000 0001] trans: [0000 0001] Complement: [0000 0001]
  3. Anti negative sign bit original code symbol is unchanged, the other bit inversion, are inverted complement +1 => -1 Original: [1000 0001] trans: [1111 1110] Complement: [1111 1111]
  4. ★★★ computer runs without subtraction, all calculations are complement carried out, i.e. 1-1 = 0 is not, but 1 + (- 1) = 0

Bitwise Operators

  1. Five operators: Five operators are binary operators
    &: and converted to a binary bit about numbers, and the same position, the same as the result of 1 to 1, all other things being zero.
    |: Bitwise or about digital conversion to binary, or parity, there is a 1 result of 1, 0 are all zero.
    ^: Bitwise XOR, about converted to binary numbers, the parity XOR, 10, 01 compared to 1, with 1 and 0 with 0.
    >>: the left into binary numbers, the number of bits to the right => (right number), the high discard, low 0s, multiplied by 2 ^ n.
    <<: the left into binary numbers, the number of bits to the left => (right number), the high bit of 0, the low discarded, divided by 2 ^ n.
    Case:

     package main
     
     import "fmt"
     
     func main() {
     var (
     	a int = 2&3
     	b int = 2|3
     	c int = 2^3
     	d int = 2>>1
     	e int = 2<<2
     )
     fmt.Printf("%b %b %b %b %b",a,b,c,d,e)
     //结果如下:10 11 1 1 1000 => 2 3 1 1 4
     }
    

Comparison Operators

  1. The result is a Boolean value type, only True and False
  2. Or if the structure is often used in the cyclic structure.
  3. Expression consisting of relational operators relational expression, can not be written == =, == is determined whether the representative equal assignment = representatives, different things children

Logical Operators

  1. Connected to a number of conditions, in general, it is relational expression, the end result is a Boolean value.

  2. Py with the same
    logic: && are true on both sides of expression is True, a false and false;
    logic or: || both sides of expression is both false and false, the other true;
    logic non-:! expression is false, logical not true, the expression is true, the logical NOT is false. Negate

  3. And short-circuit: the two conditions, if the first condition is not met, then the latter will not be judged that the conditions, is a direct result of the program False
    IF 10 <&&. 9 {...}. 1. 1 ==:. 1. 1 are not to judge == because 10 <9've been wrong.
    4, short circuit or: two conditions, the first condition is satisfied, then the latter will not be judged that the conditions, is a direct result of the program True
    IF 10> 9 || FUNC () {...}: Since 10> 9 has true , behind func () call will not be executed, and the result is True.

Published 50 original articles · won praise 18 · views 4022

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/89604520