-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
41 lines (34 loc) · 984 Bytes
/
helpers.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
package bitstring
import (
"fmt"
"strings"
)
func sprintbuf(b []byte) string {
var sb strings.Builder
for i := range b {
fmt.Fprintf(&sb, "%08b ", b[i])
}
return sb.String()
}
func printbuf(b []byte) {
fmt.Println(sprintbuf(b))
}
// returns a string representing the first n bits of the base-2 representation
// of val (unsigned).
func sprintubits(val uint64, nbits int) string {
return fmt.Sprintf(fmt.Sprintf("%%0%db", nbits), val)
}
// returns a string representing the first n bits of the base-2 representation
// of val (signed).
func sprintsbits(val int64, nbits int) string {
if val < 0 {
// casting to uint will show us the 2's complement
return sprintubits(uint64(val), nbits)
}
return fmt.Sprintf(fmt.Sprintf("%%0%db", nbits), val)
}
// prints a string representing the first n bits of the base-2 representation of val.
//lint:ignore U1000 (unused but useful for debugging)
func printbits(val, n int) {
fmt.Printf(fmt.Sprintf("%%0%db\n", n), val)
}