Pergunta

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

Foi útil?

Solução

int takes a base parameter:

>>> int('1001', 2)
9
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top