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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!