-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
36 lines (29 loc) · 786 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import "fmt"
type Bits uint32
// https://yourbasic.org/golang/bitmask-flag-set-clear/
func main(){
const (
First Bits = 1 << iota //1
Second //2
Third //4
Fourth //8
Fifth //16
Sixth //32
Seventh //64
Eighth //128
Ninth //256
)
var status Bits
//status |= ParentAuction
status |= First
status |= Second
fmt.Println("status First, Second: ", status) //6
fmt.Printf("status has First: %t\n",Has(status, First))
fmt.Printf("status has Second: %t\n",Has(status, Second))
fmt.Printf("status has Third: %t\n",Has(status, Third))
}
func Set(b, flag Bits) Bits { return b | flag }
func Clear(b, flag Bits) Bits { return b &^ flag }
func Toggle(b, flag Bits) Bits { return b ^ flag }
func Has(b, flag Bits) bool { return b&flag != 0 }