def pad(value, digits, to_right=False):
    """Only use for positive binary numbers given as strings.
    Pads to the left by default, or to the right using to_right flag.

    Inputs: value -- string of bits
            digits -- number of bits in representation
            to_right -- Boolean, direction of padding

    Output: string of bits of length 'digits'

    Raises exception if value is larger than digits in length.

    Example:
      pad('0010', 6) -> '000010'
      pad('0010', 6, True) -> '001000'
    """
    len_val = len(value)
    assert len_val <= digits
    rem_digits = digits - len_val
    if to_right:
        return value + "0"*rem_digits
    else:
        return "0"*rem_digits + value