day02-04op

运算符

var (
a = 5
b = 2
)
fmt.Println(a + b )
fmt.Println(a - b )
fmt.Println(a * b )
fmt.Println(a / b )
fmt.Println(a % b)
a++ //单独的语句,不能放在=右边赋值 a = a + 1
b-- //单独的语句,不能放在=右边赋值 b = b - 1

//关系运算符
fmt.Println(a == b)
fmt.Println(a != b)
fmt.Println(a >= b)
fmt.Println(a <= b)
fmt.Println(a >b)

//逻辑运算符
age := 22
if age > 18 && age < 60 {
fmt.Println("")
}else{
fmt.Println("")
}

if age < 18 || age > 60 {
fmt.Println("")
}else {
fmt.Println("")
}

//not取反,原来为真就为假,原来为假就为真
isMarried := false
fmt.Println(isMarried)
//false
fmt.Println(!isMarried)
//true

//位运算:针对的是二进制
//5的二进制:101
//2的二进制: 10
//&: 按位与(两位均为1才为1)
fmt.Println(5 & 2)
//000
// |:按位或(两位有1个1就为1)
fmt.Println(5 | 2)
//111
// ^:两位不一样则为1
fmt.Println(5 ^ 2)
//111
// <<:将二进制左移指定位数
fmt.Println(5 << 1) //1010
//10
// >>:将二进制右移指定位数
fmt.Println(5 >> 1) //10
//2

//赋值运算
var x int
x = 10
x += 1 // x = x+1
x -= 1
x *= 1
x /=1
x %=2

x <<= 2
x &= 2
x |= 2
x ^= 4
x >> = 2

你可能感兴趣的:(day02-04op)