aboutsummaryrefslogtreecommitdiff
path: root/utils/bitreader.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils/bitreader.go')
-rw-r--r--utils/bitreader.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/utils/bitreader.go b/utils/bitreader.go
new file mode 100644
index 0000000..c4936fd
--- /dev/null
+++ b/utils/bitreader.go
@@ -0,0 +1,41 @@
1package utils
2
3import (
4 "fmt"
5 "math"
6 "strconv"
7)
8
9func ReadBitStateLSB(input byte, index int) (bool, error) {
10 if index < 0 && index > 7 {
11 return false, fmt.Errorf("IndexOutOfBounds for type byte")
12 }
13 value := input & (1 << index)
14 return (value > 0), nil
15}
16
17func Read32BitsAfterFirstBitInt32(input []byte, index int, step int) int32 {
18 binary := ""
19 binary += fmt.Sprintf("%08b", input[step])[8-index : 8]
20 binary += fmt.Sprintf("%08b", input[step-1])
21 binary += fmt.Sprintf("%08b", input[step-2])
22 binary += fmt.Sprintf("%08b", input[step-3])
23 binary += fmt.Sprintf("%08b", input[step-4])[:8-index]
24 output, err := strconv.ParseInt(binary, 2, 32)
25 CheckError(err)
26 return int32(output)
27
28}
29
30func Read32BitsAfterFirstBitFloat32(input []byte, index int, step int) float32 {
31 binary := ""
32 binary += fmt.Sprintf("%08b", input[step])[8-index : 8]
33 binary += fmt.Sprintf("%08b", input[step-1])
34 binary += fmt.Sprintf("%08b", input[step-2])
35 binary += fmt.Sprintf("%08b", input[step-3])
36 binary += fmt.Sprintf("%08b", input[step-4])[:8-index]
37 output, err := strconv.ParseUint(binary, 2, 32)
38 CheckError(err)
39 return math.Float32frombits(uint32(output))
40
41}