+ 13

What does bin() do in python?

How to use bin()? What does bin() do? Where to use it?

26th Oct 2020, 11:38 AM
Si Thu Tun
Si Thu Tun - avatar
1 Answer
+ 2
Python's bin function converts a number to a binary string. In other words, it formats an integer as a sequence of 1 and 0 digits. I rarely need a binary representation of integers in applications I've written. It might be useful for illustrating flags or bit masks in an integer. I made an assembler many years ago and had a file describe most of Intel's 8086 instruction set in binary. Binary was useful for the instruction set because it was easy to show if 5 bits were identified an opcode and 3 bits identified a register. I think machine code for something like "inc eax"(add 1 to register eax) was formatted like this. Hexadecimal(base 16) would work only if everything lined up in groups of 4 binary digits. If you're not familiar with number bases and number base conversion, study them. After learning what number bases are, you can understand what base 2 is. Binary is just a base 2 representation of a number.
2nd Dec 2020, 11:49 AM
Josh Greig
Josh Greig - avatar