aboutsummaryrefslogtreecommitdiff
path: root/utils/bitreader.go
diff options
context:
space:
mode:
authorBiSaXa <1669855+BiSaXa@users.noreply.github.com>2022-08-29 03:21:51 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-09-16 21:39:35 +0300
commitde673b3264c68926c693e3714c70761a3bcedf1c (patch)
tree3bc2802187e63391c4abde602a70a19a76615727 /utils/bitreader.go
parentnew readme badges (diff)
downloadsdp.go-de673b3264c68926c693e3714c70761a3bcedf1c.tar.gz
sdp.go-de673b3264c68926c693e3714c70761a3bcedf1c.tar.bz2
sdp.go-de673b3264c68926c693e3714c70761a3bcedf1c.zip
janky bitreader, usercmdinfo almost finished
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}