github.com/bluenviron/mediacommon@v1.9.3/pkg/bits/write.go (about) 1 package bits 2 3 // WriteBits writes N bits. 4 func WriteBits(buf []byte, pos *int, bits uint64, n int) { 5 res := 8 - (*pos & 0x07) 6 if n < res { 7 buf[*pos>>0x03] |= byte(bits << (res - n)) 8 *pos += n 9 return 10 } 11 12 buf[*pos>>3] |= byte(bits >> (n - res)) 13 *pos += res 14 n -= res 15 16 for n >= 8 { 17 buf[*pos>>3] = byte(bits >> (n - 8)) 18 *pos += 8 19 n -= 8 20 } 21 22 if n > 0 { 23 buf[*pos>>3] = byte((bits & (1<<n - 1)) << (8 - n)) 24 *pos += n 25 } 26 }