Assuming you want the last 8 bits of your result, the simple solution is just to use modular arithmetic and use % 256
to get the remainder after dividing by 256.
def BinaryToDecimal(binaryNum):
binaryList = []
decimalNumber = 0
while binaryNum != 0:
remainder = binaryNum % 10
binaryList.append(remainder)
binaryNum = int(binaryNum / 10)
for i in range(len(binaryList) - 1, -1, -1):
decimalNumber = decimalNumber + binaryList[i] * (2 ** i)
return decimalNumber % 256 # You also had a typo in this line
For example:
BinaryToDecimal(11111100000000)
Out[1]: 0
BinaryToDecimal(11111111111111)
Out[2]: 255
Alternatively, if you want to return 255 if the answer is > 255, the code would be:
return decimalNumber if decimalNumber < 256 else 255
This returns:
BinaryToDecimal(10000000)
Out[3]: 128
BinaryToDecimal(100000000)
Out[4]: 255
BinaryToDecimal(1000000000000)
Out[5]: 255
Incidentally: I fixed a typo in the return
statement and your variable names don't conform to the PEP-8 Python styles - variables should be in lower case separated by underscores like binary_num
and decimal_number