-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmask.go
51 lines (44 loc) · 1.14 KB
/
bitmask.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
package t67xx
/*
fmt.Println(Bitmask(0x6).IsSet(0x2))
fmt.Println(Bitmask(f.FileHeader.Characteristics).ListDescriptions(charValues))
fmt.Println(Bitmask(f.FileHeader.Characteristics).ListValues(charValues))
*/
// Bitmask represents a bitmask value
type Bitmask uint16
// BitValue is a value and a description
type BitValue struct {
value Bitmask
description string
}
// ListDescriptions returns a string array of all set bits
func (value Bitmask) ListDescriptions(values []BitValue) []string {
list := make([]string, 0)
currentValue := value
for _, bv := range values {
if currentValue&bv.value != 0 {
currentValue ^= bv.value
list = append(list, bv.description)
}
}
return list
}
// ListValues returns a string array of all set bits
func (value Bitmask) ListValues(values []BitValue) []Bitmask {
list := make([]Bitmask, 0)
currentValue := value
for _, bv := range values {
if currentValue&bv.value != 0 {
currentValue ^= bv.value
list = append(list, bv.value)
}
}
return list
}
// IsSet returns true if a bit is set
func (value Bitmask) IsSet(test Bitmask) bool {
if value&test != 0 {
return true
}
return false
}