/
flags.go
57 lines (45 loc) · 1.11 KB
/
flags.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package bitwise
import (
"fmt"
"math/bits"
"strconv"
)
// Flags represents a pool of settable flags (64)
type Flags uint64
/*
Information
*/
// Count returns the number of flags (bits) set in "pool" (Hamming weight)
func (pool Flags) Count() int {
return bits.OnesCount64(uint64(pool))
}
// IsFlagSet returns true if the flag "flag" is present within "pool" (AND)
func (pool Flags) IsFlagSet(flag Flags) bool {
return pool&flag == flag
}
/*
Manipulation
*/
// SetFlag activate "flag" within "pool" (OR)
func (pool *Flags) SetFlag(flag Flags) {
*pool = *pool | flag
}
// ToggleFlag switch the state of "flag" within "pool" (XOR)
func (pool *Flags) ToggleFlag(flag Flags) {
*pool = *pool ^ flag
}
// UnsetFlag unset "flag" value within "pool" (AND NOT)
func (pool *Flags) UnsetFlag(flag Flags) {
*pool = *pool &^ flag
}
/*
Printing
*/
// String implements the stdlib Stringer interface
func (pool Flags) String() string {
return strconv.FormatUint(uint64(pool), 2)
}
// GoString implements the stdlib GoStringer interface (use "%#v")
func (pool Flags) GoString() string {
return fmt.Sprintf("%064b(%d)", pool, pool)
}