def get_bit(data, bit_index):
    """
    Get bit as integer at index bit_index from bytes array.
    Order is from left to right, so bit_index=0 is MSB.
    """
    if bit_index < 8 * len(data):
        cur_byte = data[bit_index // 8]
        return (cur_byte >> (7 - bit_index % 8)) & 0x1
    raise RuntimeError("Out of bound index %s" % bit_index)