type Decoder struct { fr []byte at uint32 lo uint32 hi uint32 } func NewDecoder(from []byte) *Decoder { var ( at1 = uint32(from[0]) << 24 at2 = uint32(from[1]) << 16 at3 = uint32(from[2]) << 8 at4 = uint32(from[3]) ) return &Decoder{ fr: from[4:], at: at1 + at2 + at3 + at4, hi: ^uint32(0), } } func (d *Decoder) ReadBit(probability1 uint16) bool { var ( diff = uint64(d.hi - d.lo) prob = uint64(probability1) add = diff * prob >> 16 mid = d.lo + uint32(add) is1 = d.at <= mid ) if is1 { d.hi = mid } else { d.lo = mid + 1 } for d.lo^d.hi <= 0x_ff_ff_ff { d.lo <<= 8 d.hi <<= 8 d.hi += 0xff d.at <<= 8 d.at += uint32(d.fr[0]) } return is1 }
To receive a hint, submit unfixed code.