Link to home
Start Free TrialLog in
Avatar of Kyle Hamilton
Kyle HamiltonFlag for United States of America

asked on

bitwise OR python 2.7

I would like to perform a bitwise OR on two string representations of binary numbers, but I can't figure out how to convert the strings into raw binary.

a = '010110'
b = '100000'

a | b

should yield: 110110

I then want to count the number of on bits.
This should return:
4
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

This page may help: https://docs.python.org/2/library/stdtypes.html#additional-methods-on-integer-types .  '0b' is the prefix for binary numbers.
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kyle Hamilton

ASKER

thank you aikimark. that's exactly what i ended up doing.

seems sort of unsatisfactory that i end up with a string to iterate over though. i was hoping for a more "efficient" way to count the on bits. (bit_length) is just the length of the string)
There is probably a library/package, but I don't have any recommendations at this moment.