+ 1
Can anyone help me to understand the meaning of this statement "n1 = int(n1, 2)".
In above statement the input is 1001 in string format and it shows output as "9". I can't able to find how?
2 Answers
+ 4
When you provide 2 as an extra parameter to int(), it's for the number base. A number base of 2 is basically binary mode, in which numbers can either be 0 or 1. The placeholders look like this:
#8 | 4 | 2 | 1
So, if you provide it with 1001, 8 + 1(the two 1s) is 9, meaning the output is 9.
+ 1
int(n,2) convert the binary no to decimal no.bin(n) convert decimal no to binary no. By simply changing the x in int(n,x), you can get no in different format like 2 for decimal, 8 for octal, 16 for hexadecimal. Check out the function oct(),hex()