+ 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?

3rd Jan 2020, 1:50 AM
Dileep Patcha
Dileep Patcha - avatar
2 ответов
+ 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.
3rd Jan 2020, 1:57 AM
Jianmin Chen
Jianmin Chen - avatar
+ 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()
3rd Jan 2020, 3:03 AM
Geek
Geek - avatar