1

I'm using Python v3.6.3 to convert a integer to its binary representation (ex bin(12)='0b1100' and vice versa (ex int('0b1100',2)=12). I would like to know what algorithm is used. Can anybody clue me in?

Zoe L
  • 1,150
  • 14
  • 22
  • If you're looking for the decimal->binary and binary->decimal conversion logic, that all happens when the bytecode compiler parses the `12` literal or when Python prints the int. It's not actually part of the `int` or `bin` calls you've posted. – user2357112 Aug 06 '18 at 06:13

1 Answers1

0

You can refer to CPython's source. Look for long_format_binary's definition:

https://github.com/python/cpython/blob/master/Objects/longobject.c

blhsing
  • 91,368
  • 6
  • 71
  • 106