5

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

Rishabh Ryber
  • 446
  • 1
  • 7
  • 21

1 Answers1

11

Try this:

int(s, base=2)

for example,

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

@metatoaster also mentioned this.

Calder White
  • 1,287
  • 9
  • 21