go learn the fifth day, operators

Arithmetic Operators

The following table lists all arithmetic operators Go language. Assuming that A is 10, B is 20

Operators description Examples
+ Adding A + B output 30
- Subtraction A - B output -10
* Multiplied A * B output 200
/ Divided B / A output 2
% Remainder B% A 0 output
++ Increment The output 11 A ++
Decrement A- output 9

Go language is no front + + - -
for example: (+ + a, - -a ) this is wrong, not supported

Relational Operators

The following table lists the relational operators Go all languages. Assuming that A is 10, B is 20.

Operators description Examples
== Check the two values ​​are equal, equal returns True if otherwise False. (A == B) to False
!= Check the two values ​​are not equal, not equal returns True if otherwise False. (A! = B) is True
> Check the left value is greater than the right value, it returns True if it is otherwise False. (A> B) is False
< Check the left value is less than the right value, returns True if it is otherwise False. (A < B) 为 True
>= Check the left value is greater than or equal to the right value, it returns True if it is otherwise False. (A> = B) to False
<= Check the left value is less than or equal to the right value, returns True if it is otherwise False. (A <= B) 为 True

== compares with an array

  • The same dimensions and containing the same number of elements the array can compare
  • Each element of the same are equal

Logical Operators

The following table lists all logical operators in Go. Assuming that A is True, B is False.

Operators description Examples
&& Logical AND operator. If both sides of the operands are True, the conditions are True, otherwise False. (A && B) 为 False
|| Logical OR operator. If the number of operations on both sides of a True, the conditions are True, otherwise False. (A || B) is True
! 逻辑 NOT 运算符。 如果条件为 True,则逻辑 NOT 条件 False,否则为 True。 !(A && B) 为 True

位运算符

Go 语言支持的位运算符如下表所示。假定 A 为60,B 为13:

运算符 描述 实例
& 按位与运算符”&”是双目运算符。 其功能是参与运算的两数各对应的二进位相与。 (A & B) 结果为 12, 二进制为 0000 1100
按位或运算符” “是双目运算符。 其功能是参与运算的两数各对应的二进位相或 (A B) 结果为 61, 二进制为 0011 1101
^ 按位异或运算符”^”是双目运算符。 其功能是参与运算的两数各对应的二进位相异或,当两对应的二进位相异时,结果为1。 (A ^ B) 结果为 49, 二进制为 0011 0001
<< 左移运算符”<<”是双目运算符。左移n位就是乘以2的n次方。 其功能把”<<”左边的运算数的各二进位全部左移若干位,由”<<”右边的数指定移动的位数,高位丢弃,低位补0。 A << 2 结果为 240 ,二进制为 1111 0000
>> 右移运算符”>>”是双目运算符。右移n位就是除以2的n次方。 其功能是把”>>”左边的运算数的各二进位全部右移若干位,”>>”右边的数指定移动的位数。 A >> 2 结果为 15 ,二进制为 0000 1111

&^按位置零

1 &^ 0 -- 1
1 &^ 1 -- 0
0 &^ 1 -- 0
0 &^ 0 -- 0
const (
    Readable  = 1 << iota //0001
    Writable              //0010
    Exectable             //0011
)

func TestBitClear(t *testing.T) {
    a := 7 // 0111
    a = a &^ Readable
    a = a &^ Exectable
    t.Log(a&Readable == Readable, a&Writable == Writable, a&Exectable == Exectable)
}

输出

=== RUN   TestBitClear
--- PASS: TestBitClear (0.00s)
    operator_test.go:25: false true false
PASS

Process finished with exit code 0

Guess you like

Origin www.cnblogs.com/zhangwenjian/p/12037743.html