6

I have a string of 6 characters (only 0s and 1s)on which I have to use binary operations. I came across a few pages but most mentioned converting characters to binary and appending them to give the final result.

I have to use it in this function

def acmTeam(topic):
    global finalCount
    for i in range(len(topic)-1):
        for j in range(i+1,len(topic)):
            final=toBinary(topic[i])|toBinary(topic[j])
            print(final)

One example value of topic is

['10101', '11100', '11010', '00101']

and i want 10101 and 11100 binary values

Here I can create my own function toBinary to convert it to binary equivalent and return, but is there any inbuilt function or more efficient way to do so in python?

Thanks in advance :)

6
  • "i want 10101 and 11100 binary values" - what does it mean? Commented Jul 16, 2019 at 3:41
  • 3
    int('10101', base=2) vs "{0:b}".format(21) Commented Jul 16, 2019 at 3:42
  • @CalderWhite here i have series of 0 and 1's not integer as input Commented Jul 16, 2019 at 3:42
  • This would be the duplicate: Convert base-2 binary number string to int Commented Jul 16, 2019 at 3:43
  • @alfasin something that can give int(10101)->21 Commented Jul 16, 2019 at 3:44

1 Answer 1

14

Try this:

int(s, base=2)

for example,

for i in ['10101', '11100', '11010', '00101']:
    print(int(i, base=2))

@metatoaster also mentioned this.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.