# USER INPUTS:
# Vector with all the ids that should be considered

idsVector = [int (i) for i in input("Enter the vector with all ids to be taken into account for calculating the mask: ").split(',')]

print('')

# Flag to indicate if the CAN Ids are extended [1] or standard [0]

extendedFlag = float(input("Indicate wheter the CAN IDs are extended [1] or standard [0]: "))


# CODE
if extendedFlag:
    idLength = 29
else:
    idLength = 11

binIdsVector = idsVector.copy()

for id in idsVector:
    index = idsVector.index(id)
    splittedBinaryId = bin(id).split('b')

    lengthDifference = idLength - len(splittedBinaryId[1])

    if lengthDifference > 0:
        addZeros = ''
        for ii in range(lengthDifference):
            addZeros = addZeros + '0'
        binaryId = addZeros + splittedBinaryId[1]
    elif lengthDifference == 0:
        binaryId = splittedBinaryId[1]
    else:
        raise Exception("ID "+str(id)+" is longer than expected in binary form")
    binIdsVector[index] = binaryId

print('')
print("--------------------------------------------------------------------------")
print('')

mask = ''

for ii in range(idLength):
    selectedBit = binIdsVector[1][ii]
    comparison = 0

    for jj in range(len(binIdsVector)):
        compareBit = binIdsVector[jj][ii]
        comparison = selectedBit == compareBit
        if not comparison:
            break

    if comparison:
        mask = mask + '1'
    else:
        mask = mask + '0'

for ii in range(len(binIdsVector)):
    strToPrint = "ID: " + str(idsVector[ii]) + " -> ID in binary: " + binIdsVector[ii]
    print(strToPrint)

print('')
print("--------------------------------------------------------------------------")
print('')
print("Calculated mask -> " + mask)











