def bits_to_bytes(bit_array, bpb=8):
    """Convert an array or string of bits to bytes."""
    if len(bit_array) % bpb != 0:
        raise ValueError('Number of bits must be a multiple of ' + str(bpb))
    result = b''
    for i in range(0, len(bit_array), bpb):
        bits = [str(bits) for bits in bit_array[i:i + bpb]]
        bitstring = ''.join(bits)
        result += bytes([int(bitstring, 2)])
    return result
