Ruby bit banging, how to perform additive and negative
I have a 2 collections of bitmasks, 1 has permissions that I want to combine, and the other collection has bitmasks I want to remove.
For additive, I just 'OR' them like:
permissions = 0
add_masks.each do |x|
permissions |= permissions
end
How do I remove permissions using the other collection?
开发者_JS百科remove_masks.each do |x|
???
end
You'll want to use permissions &= ~mask
:
irb > permissions = 0
# => 0
irb > permissions |= 512
# => 512
irb > permissions |= 256
# => 768
irb > permissions &= ~1
# => 768
irb > permissions &= ~256
# => 512
irb > permissions &= ~512
# => 0
~(~a|b)
is bitwise subtraction of b from a
精彩评论