You have Integer#to_s(base)
and String.to_i(base)
available to you.
a_and_b = (a.to_i(2) & b.to_i(2)).to_s(2)
a_or_b = (a.to_i(2) | b.to_i(2)).to_s(2)
Pergunta
I have an array of long(45) character bitstrings, that basically represent the boolean state of a bunch of attributes. I need to perform OR and AND operations on these strings and get back the resulting binary string. ex:
a = "100"
b = "101"
I now need to compute:
a_or_b = "101"
a_and_b = "100"
Is there efficient anyway for me to do this? Can I use the bitwise operators somehow?
Solução
You have Integer#to_s(base)
and String.to_i(base)
available to you.
a_and_b = (a.to_i(2) & b.to_i(2)).to_s(2)
a_or_b = (a.to_i(2) | b.to_i(2)).to_s(2)