Search code examples
python-2.7binarybin

Converting a binary string to a binary number as to perform binary arithmetic


In Python 2.7 I am trying to a covert a String, eg '1001' to the binary number '1001' so that I can perform binary arithmetic on it.

Specifically I want to add bin(1) and left shift the binary string.

Is there an easy way to do this (preferably in the python library)?


Solution

  • int takes a base parameter:

    >>> int('1001', 2)
    9