import six
import ipaddress


def address_to_bin(address, split=None):
    """Converts an dotted-decimal (IPv4) or groups of hexadecimals (IPv6)
    formatted address to a binary representation

    :param address: The IP address
    :type address: ipaddress.IPv4Address or ipaddress.IPv6Address or str
    :param int split: Position to split the address
    :return: A binary representation of the IP address
    :rtype: str
    """

    if isinstance(address, six.string_types):
        address = ipaddress.ip_address(six.u(address))

    if address.version == 4:
        bits_total = 32
        bits_step = 8
        bits_sep = '.'
    else:
        bits_total = 128
        bits_step = 16
        bits_sep = ':'

    bits_array = bin(int(address))[2:]
    bits_array = bits_array.zfill(bits_total)
    bits_array = [bits_array[p:p + bits_step] for p in
                  range(0, bits_total, bits_step)]
    bits_array = bits_sep.join(bits_array)

    if split:
        split += split // bits_step
        bits_array = bits_array[:split] + ' ' + bits_array[split:]

    return bits_array