-
Notifications
You must be signed in to change notification settings - Fork 11
/
bitwriter.go
80 lines (70 loc) · 1.64 KB
/
bitwriter.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
package bits
import "strings"
/**
The BitWriter will create a stream of bytes, letting you write a certain
number of bits at a time. This is part of the encoder, so it is not
optimized for memory or speed.
*/
type BitWriter struct {
bits []uint
}
/**
Write some data to the bit string. The number of bits must be 32 or
fewer.
*/
func (bw *BitWriter) Write(data, numBits uint) {
//for i := (numBits-1); i >= 0; i-- {
// @siongui: the above commented line will cause infinite loop, why???
// answer from @xphoenix:
// Because i becomes uint, let's check iteration when i == 0, at the end
// of loop, i-- takes place but as i is uint, it leads to 2^32-1 instead
// of -1, loop condition is still true...
for i := numBits; i > 0; i-- {
j := i - 1
if (data & (1 << j)) != 0 {
bw.bits = append(bw.bits, 1)
} else {
bw.bits = append(bw.bits, 0)
}
}
}
/**
Get the bitstring represented as a javascript string of bytes
*/
func (bw *BitWriter) GetData() string {
var chars []string
var b, i uint = 0, 0
for j := 0; j < len(bw.bits); j++ {
b = (b << 1) | bw.bits[j]
i += 1
if i == W {
chars = append(chars, CHR(b))
i = 0
b = 0
}
}
if i != 0 {
chars = append(chars, CHR(b<<(W-i)))
}
return strings.Join(chars, "")
}
/**
Returns the bits as a human readable binary string for debugging
*/
func (bw *BitWriter) GetDebugString(group uint) string {
var chars []string
var i uint = 0
for j := 0; j < len(bw.bits); j++ {
if bw.bits[j] == 1 {
chars = append(chars, "1")
} else {
chars = append(chars, "0")
}
i++
if i == group {
chars = append(chars, " ")
i = 0
}
}
return strings.Join(chars, "")
}