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

StackOverflow https://stackoverflow.com/questions/16332051

  •  14-04-2022
  •  | 
  •  

سؤال

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

هل كانت مفيدة؟

المحلول

int takes a base parameter:

>>> int('1001', 2)
9
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top