If there is an binary number:10011100
It is 156 in decimal.
I want to use mathematics way to make binary to decimal.
For example:
binary: 10011100
the first number is 1: 2**7
the forth number is 1: 2**4
the fifth number is 1: 2**3
the sixth number is 1: 2**2
then 2**7+2**4+2**3+2**2 = 156
I think, I need to use string.find() method.
>>> my_str = '10011100'
>>> my_str = my_str[::-1]
>>> print(my_str)
00111001
>>> my_str.find('1')
2
>>>
I just can find the first ‘1’.
How to find all the index of ‘1’?
Why do you want to retrieve the indexes? You can simply iterate over the bits like this:
Anyway, you can get the indexes like this if you prefer two separate steps: