def bit2byte(in_bits):
    """
    Function for converting a bit stream into the corresponding byte stream.
    Original Author: Filippo Valmori
    """
    in_len = len(in_bits)
    out_len = in_len >> 3
    out_bytes = [0] * out_len
    for j in range(in_len):
        if in_bits[j]:
            byte_idx = j >> 3
            bit_idx = 7 - j % 8
            out_bytes[byte_idx] += 1 << bit_idx
    return out_bytes
