Question
Hello, Can you explain this code? It supposed to convert an 8 digit binary number to a decimal number and the code is correct but
Hello, Can you explain this code? It supposed to convert an 8 digit binary number to a decimal number and the code is correct but I just can't understand it.
# converting binary to decimal d=0 count=0
for b in binary[::-1]: d+=int(b)*(2 ** count) count+=1
Question: what [::-1] means? Question: what (2**count) means?
Thanks! ORIGINAL CODE import sys
# getting user input of binary number binary=input("Please enter a 8-bit binary number: ")
# checking for binary length if len(binary) >8 or len(binary) < 8: print('Enter a valid 8-bit binary number.') sys.exit()
# checking for valid binary digits for d in binary:
if d=='0' or d == '1': continue else: print('Binary number can contain combinations of 1s and 0s.' ) sys.exit()
# converting binary to decimal d=0 count=0
for b in binary[::-1]: d+=int(b)*(2 ** count) count+=1
# displaying the equivalent decimal number print('The decimal equivalent of %s is %d' %(binary,d))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started