05. int()---Why print int("11001001", 2) is "201"


#1


Is there anyone can tell me why:

print int("11001001", 2) result is "201"? How to calculate it?

Thanks


Replace this line with your code.


#2

Sure, the calculations are quite straightforward:

bin(11001001) = dec(1 * 2^7 + 1 * 2^6 + 0 * 2^5 + 0 * 2^4 + 1 * 2^3 + 0 * 2^2 + 0 * 2^1 + 1 * 2^0)

So every digit of a binary number represents a power of 2. We know that 0 * n = 0, so we are only interested in ones:

bin(11001001) = dec(1 * 2^7 + 1 * 2^6 + 1 * 2^3 + 1 * 2^0) =
dec(128 + 64 + 8 + 1) = dec(201)

You can learn more about binary numbers here -> https://en.wikipedia.org/wiki/Binary_number.


#3

The second parameter returns the first parameter's base ten value which in this case is 201,

base ten value with a second parameter whose value is 2,

"201"


#4

In addition to factoradic
Every bit is the n_th power of 2

2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16

" 1 1 0 0 1 0 0 1"
| | | | | | | |
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0

" 1 1 0 0 1 0 0 1"
| | | | | | | |
128 64 32 16 8 4 2 1

then make a sum of the 1 bits.

" 1 1 0 0 1 0 0 1"
| | | | | | | |
128*1|--------------------------------> 128
64 *1---------------------------> 64
32*0-----------------------> 0
16*0------------------> 0
8*1---------------> 8
4*0----------> 0
2*0-------> 0
1*1----> 1
+_____
201


#5

Thank you very much. Very clear!


#6

Nice answer, thank you.


#7

Thank you very much, bandit.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.