-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits.go
59 lines (54 loc) · 1.29 KB
/
bits.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
package vcsgo
func reverseByte(b byte) byte {
out := byte(0)
for i := 0; i < 8; i++ {
out <<= 1
out |= b & 1
b >>= 1
}
return out
}
func boolByte(b bool) byte {
if b {
return 1
}
return 0
}
func boolBit(bNum byte, b bool) byte {
if b {
return 1 << bNum
}
return 0
}
func ifBool(b bool, fn func()) {
if b {
fn()
}
}
func byteFromBools(b7, b6, b5, b4, b3, b2, b1, b0 bool) byte {
var result byte
ifBool(b7, func() { result |= 0x80 })
ifBool(b6, func() { result |= 0x40 })
ifBool(b5, func() { result |= 0x20 })
ifBool(b4, func() { result |= 0x10 })
ifBool(b3, func() { result |= 0x08 })
ifBool(b2, func() { result |= 0x04 })
ifBool(b1, func() { result |= 0x02 })
ifBool(b0, func() { result |= 0x01 })
return result
}
func ifBoolPtrNotNil(bptr *bool, fn func()) {
if bptr != nil {
fn()
}
}
func boolsFromByte(val byte, b7, b6, b5, b4, b3, b2, b1, b0 *bool) {
ifBoolPtrNotNil(b7, func() { *b7 = val&0x80 > 0 })
ifBoolPtrNotNil(b6, func() { *b6 = val&0x40 > 0 })
ifBoolPtrNotNil(b5, func() { *b5 = val&0x20 > 0 })
ifBoolPtrNotNil(b4, func() { *b4 = val&0x10 > 0 })
ifBoolPtrNotNil(b3, func() { *b3 = val&0x08 > 0 })
ifBoolPtrNotNil(b2, func() { *b2 = val&0x04 > 0 })
ifBoolPtrNotNil(b1, func() { *b1 = val&0x02 > 0 })
ifBoolPtrNotNil(b0, func() { *b0 = val&0x01 > 0 })
}