def num_to_bin(num, nbits):
    """From decimal number to binary array.
    num : decimal number
    nbits :  number of bits for the output array

    The most significant digit is located in the last element of the binarray
    """
    binary = []
    while num != 0:
        bit = num % 2
        binary.append(bit)
        num = num // 2

    binarray = [0 for x in range(nbits)]
    Nbits = len(binary)
    for i in range(Nbits):
        binarray[nbits - i - 1] = binary[i]

    return binarray