def from_binary(base256_binary_str, num_bits):
    """
    Return new BitList from base256_binary_str and number of bits.
    """
    ans = BitList()
    if len(base256_binary_str) != (num_bits+7)//8:
      raise ValueError('invalid length')
    ans.length = int(num_bits)
    ans.data = array.array('B')
    ans.data.fromstring(base256_binary_str)
    return ans

    Return new BitList from base256_binary_str and number of bits.
    
recipes/Python/440658_Memory_compacted_list_of_bits/recipe-440658.py