There are only 10 types of people in the world: those who understand binary, and those who don’t. – Unknown
To convert decimal to binary:
Do this on paper. Stop to understand why the remainder will always be 1 or 0.
FYI: The above technique works with decimal to any base! Just change ‘2’ to the base you want to covert to.
# Returns a binary string
# you would never do this in a real program as Python has `bin(n)`
def decimal2binary(n):
nba = [] # n as binary array
div = n # dividend
while div != 0:
rem = div % 2
div = div // 2
nba.append(rem)
# list comprehension to reverse array and join elements as a string
return ''.join(str(i) for i in reversed(nba))
Day 10: Binary Numbers. 30 Days of Code. HackerRank.