开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜