[fix] BitWriter::WriteBit只取最低位

pull/2/head
q191201771 5 years ago
parent 4af89d498c
commit 8ed6c8e579

@ -55,8 +55,9 @@ func NewBitWriter(b []byte) BitWriter {
}
}
// @param b: 当b不为0和1时取b的最低位
func (bw *BitWriter) WriteBit(b uint8) {
bw.core[bw.index] |= b << (7 - bw.pos)
bw.core[bw.index] |= (b & 0x1) << (7 - bw.pos)
bw.pos++
if bw.pos == 8 {
bw.pos = 0

@ -121,6 +121,15 @@ func TestBitWriter_WriteBit(t *testing.T) {
}
assert.Equal(t, uint8(48), v[0])
assert.Equal(t, uint8(57), v[1])
v = make([]byte, 2)
bw = nazabits.NewBitWriter(v)
bs = []uint8{2, 4, 3, 5, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1}
for _, b := range bs {
bw.WriteBit(b)
}
assert.Equal(t, uint8(48), v[0])
assert.Equal(t, uint8(57), v[1])
}
func TestBitWriter_WriteBits(t *testing.T) {
@ -134,6 +143,11 @@ func TestBitWriter_WriteBits(t *testing.T) {
bw.WriteBits(1, 1)
assert.Equal(t, uint8(48), v[0])
assert.Equal(t, uint8(57), v[1])
v = make([]byte, 1)
bw = nazabits.NewBitWriter(v)
bw.WriteBits(3, 1+8+32+128)
assert.Equal(t, uint8(1<<5), v[0])
}
func BenchmarkGetBits16(b *testing.B) {

Loading…
Cancel
Save