I came across some code today for converting binary to decimal, and I realised that I don't fully understand how it works.
Here it is in Python:
#Binary to denary conversion binary = input("Input a number in binary:") denary = 0 for digit in binary: # A left shift in binary means x2 denary = denary * 2 + int(digit) print("Your denary number is: " + str(denary))
To help myself understand it I tried "converting" from base 10 to base 10. E.g.
d = 0
d *= 10, d += 2, d = 2
d *= 10, d += 4, d = 24
d *= 10, d += 6, d= 246
I'm having trouble seeing how this works for binary to decimal though. Could someone please give me a clear explanation that's thorough and easy to follow?