(n, bits=0, str=False)
Return a list of length ``bits`` corresponding to the binary value
    of ``n`` with small bits to the right (last). If bits is omitted, the
    length will be the number required to represent ``n``. If the bits are
    desired in reversed order, use the [::-1] slice of the returned list.

    If a sequence of all bits-length lists starting from [0, 0,..., 0]
    through [1, 1, ..., 1] are desired, pass a non-integer for bits, e.g.
    'all'.

    If the bit *string* is desired pass ``str=True``.

    Examples
    ========

    >>> from sympy.utilities.iterables import ibin
    >>> ibin(2)
    [1, 0]
    >>> ibin(2, 4)
    [0, 0, 1, 0]
    >>> ibin(2, 4)[::-1]
    [0, 1, 0, 0]

    If all lists corresponding to 0 to 2**n - 1, pass a non-integer
    for bits:

    >>> bits = 2
    >>> for i in ibin(2, 'all'):
    ...     print(i)
    (0, 0)
    (0, 1)
    (1, 0)
    (1, 1)

    If a bit string is desired of a given length, use str=True:

    >>> n = 123
    >>> bits = 10
    >>> ibin(n, bits, str=True)
    '0001111011'
    >>> ibin(n, bits, str=True)[::-1]  # small bits left
    '1101111000'
    >>> list(ibin(3, 'all', str=True))
    ['000', '001', '010', '011', '100', '101', '110', '111']
Return a list of length ``bits`` corresponding to the binary value
    of ``n`` with small bits to the right (last). If bits is omitted, the
    length will be the number required to represent ``n``. If the bits are
    desired in reversed order, use the [::-1] slice of the returned list.
Return a list of length bits corresponding to the binary value of n with small bits to the right ( last ) . If bits is omitted the length will be the number required to represent n . If the bits are desired in reversed order use the [ :: - 1 ] slice of the returned list .
def ibin(n, bits=0, str=False):
    """Return a list of length ``bits`` corresponding to the binary value
    of ``n`` with small bits to the right (last). If bits is omitted, the
    length will be the number required to represent ``n``. If the bits are
    desired in reversed order, use the [::-1] slice of the returned list.

    If a sequence of all bits-length lists starting from [0, 0,..., 0]
    through [1, 1, ..., 1] are desired, pass a non-integer for bits, e.g.
    'all'.

    If the bit *string* is desired pass ``str=True``.

    Examples
    ========

    >>> from sympy.utilities.iterables import ibin
    >>> ibin(2)
    [1, 0]
    >>> ibin(2, 4)
    [0, 0, 1, 0]
    >>> ibin(2, 4)[::-1]
    [0, 1, 0, 0]

    If all lists corresponding to 0 to 2**n - 1, pass a non-integer
    for bits:

    >>> bits = 2
    >>> for i in ibin(2, 'all'):
    ...     print(i)
    (0, 0)
    (0, 1)
    (1, 0)
    (1, 1)

    If a bit string is desired of a given length, use str=True:

    >>> n = 123
    >>> bits = 10
    >>> ibin(n, bits, str=True)
    '0001111011'
    >>> ibin(n, bits, str=True)[::-1]  # small bits left
    '1101111000'
    >>> list(ibin(3, 'all', str=True))
    ['000', '001', '010', '011', '100', '101', '110', '111']

    """
    if not str:
        try:
            bits = as_int(bits)
            return [1 if i == "1" else 0 for i in bin(n)[2:].rjust(bits, "0")]
        except ValueError:
            return variations(list(range(2)), n, repetition=True)
    else:
        try:
            bits = as_int(bits)
            return bin(n)[2:].rjust(bits, "0")
        except ValueError:
            return (bin(i)[2:].rjust(n, "0") for i in range(2**n))
def ibin ( n , bits = 0 , str = False ) : if not str : try : bits = as_int ( bits ) return [ 1 if i == "1" else 0 for i in bin ( n ) [ 2 : ] . rjust ( bits , "0" ) ] except ValueError : return variations ( list ( range ( 2 ) ) , n , repetition = True ) else : try : bits = as_int ( bits ) return bin ( n ) [ 2 : ] . rjust ( bits , "0" ) except ValueError : return ( bin ( i ) [ 2 : ] . rjust ( n , "0" ) for i in range ( 2 ** n ) )