def fetch_bits(bytearr, nbits, bit_offset):
    byte_offset = (bit_offset // 8)
    rem = (bit_offset % 8)
    bits = 0
    for i in range(nbits):
        bit = ((bytearr[byte_offset] | (bytearr[(byte_offset + 1)] << 8)) & (1 << (rem + i)))
        bits |= (bit >> rem)
    return bits
def fetch_bits(bytearr, nbits, bit_offset):
    byte_offset = (bit_offset // 8)
    rem = (bit_offset % 8)
    bits = 0
    for i in range(nbits):
        bit = ((bytearr[byte_offset] | (bytearr[(byte_offset + 1)] << 8)) & (1 << (rem + i)))
        bits |= (bit >> rem)
    return bits