package main
import "fmt"
const (
A = 1 << iota // 1 << 0 which is 00000001
B // 1 << 1 which is 00000010
C // 1 << 2 which is 00000100
D // 1 << 3 which is 00001000
E // 1 << 4 which is 00010000
)
func main() {
/*
print:
has A
has C
has E
*/
check(A | C | E)
}
func check(flag int) {
if flag&A == A {
fmt.Println("has A")
}
if flag&B == B {
fmt.Println("has B")
}
if flag&C == C {
fmt.Println("has C")
}
if flag&D == D {
fmt.Println("has D")
}
if flag&E == E {
fmt.Println("has E")
}
}
有疑问加站长微信联系(非本文作者)