Operators: used to represent data operations, assignment and comparison
- Arithmetic operators: + - * /% + - negative, etc.
- Assignment operator + = = - = = * / =% =
- Comparison (relationship) operator> = <= <> ==! =
- The logical operators && ||!
- Bitwise Operators
- Other operators & address-value *
Expressions concept: anything of value can be seen as an expression
Operator precedence:
- Operators have different priorities, i.e. the order of operations.
- Only unary operators, the assignment operator is right-to-left.
- 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
}
- In addition to integer and fractional addition to differences, do integer division will remove the fractional part.
- Modulo nature: a% b = a- (a / b) * b.
- Increment decrement only when the independent language, such as i ++, i-, but not a: = i ++ (i-), can not be compared i ++> 0 too.
- ★★★ increment and decrement ++ i -i not the only i ++ and i-, and praising this!
- Golang easily removed increment decrement confusingly written, the golang more concise, unified (mandatory).
Assignment Operators
- C = A + B A + B is the result of imparting C
C = A + C = C + A is
so
Ary
- 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
- Octal can support var i int = 011 this representation
- Hexadecimal support var i int = 0x11 indicates that
The original code, anti-code complement
- Signed numbers, the sign bit is a binary high, positive 1 0 negative
- Positive original code, anti-code, are the same complement => 1 Original: [0000 0001] trans: [0000 0001] Complement: [0000 0001]
- 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]
- ★★★ computer runs without subtraction, all calculations are complement carried out, i.e. 1-1 = 0 is not, but 1 + (- 1) = 0
Bitwise Operators
-
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
- The result is a Boolean value type, only True and False
- Or if the structure is often used in the cyclic structure.
- Expression consisting of relational operators relational expression, can not be written == =, == is determined whether the representative equal assignment = representatives, different things children
Logical Operators
-
Connected to a number of conditions, in general, it is relational expression, the end result is a Boolean value.
-
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 -
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.