开发者

Ruby bit banging, how to perform additive and negative

开发者 https://www.devze.com 2023-02-16 03:24 出处:网络
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.

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

精彩评论

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