Kyle Hamilton
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
a = '010110'
b = '100000'
a | b
should yield: 110110
I then want to count the number of on bits.
This should return:
4
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
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.