-
Notifications
You must be signed in to change notification settings - Fork 29
/
util.go
110 lines (99 loc) · 1.45 KB
/
util.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package util
// Bit check val's idx bit
func Bit(val interface{}, idx int) bool {
switch val := val.(type) {
case uint64:
if idx < 0 || idx > 63 {
return false
}
return (val & (1 << idx)) != 0
case uint32:
if idx < 0 || idx > 31 {
return false
}
return (val & (1 << idx)) != 0
case uint:
if idx < 0 || idx > 31 {
return false
}
return (val & (1 << idx)) != 0
case int:
if idx < 0 || idx > 31 {
return false
}
return (val & (1 << idx)) != 0
case uint16:
if idx < 0 || idx > 15 {
return false
}
return (val & (1 << idx)) != 0
case byte:
if idx < 0 || idx > 7 {
return false
}
return (val & (1 << idx)) != 0
}
return false
}
func SetBit16(val uint16, idx int, b bool) uint16 {
if b {
val = val | (1 << idx)
} else {
val = val & ^(1 << idx)
}
return val
}
func SetBit8(val byte, idx int, b bool) byte {
if b {
val = val | (1 << idx)
} else {
val = val & ^(1 << idx)
}
return val
}
func Bool2Int(b bool) int {
if b {
return 1
}
return 0
}
func Bool2U8(b bool) byte {
if b {
return 1
}
return 0
}
func Bool2U16(b bool) uint16 {
if b {
return 1
}
return 0
}
func Bool2U32(b bool) uint32 {
if b {
return 1
}
return 0
}
func Bool2U64(b bool) uint64 {
if b {
return 1
}
return 0
}
func SetMSB(val byte, b bool) byte {
if b {
val |= 0x80
} else {
val &= 0x7f
}
return val
}
func SetLSB(val byte, b bool) byte {
if b {
val |= 1
} else {
val &= 0xfe
}
return val
}