def dec2bin(n, p=1):
    """Convert a decimal value ``n`` to an array of bits (MSB first).
    Optionally, pad the overall size to ``p`` bits."""
    assert n >= 0
    if type(n) is not int:
        n = int(n)
    retval = []
    while n > 0:
        retval.append(n & 1)
        n >>= 1
    if p > 0:
        retval.extend([0] * (p - len(retval)))
    retval.reverse()
    return retval
