Bit banging in ruby

后端 未结 3 734
自闭症患者
自闭症患者 2021-02-10 13:24

I want to create a bit, that will contain security permissions for a given user.

In c#, I would do this by creating an enumeration, and then I would do some bit banging

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-10 13:44

    Bitwse operations are trivial in Ruby.

    > 1 | 2 # Create a bitmask from permission 2^0 + 2^1
    => 3
    
    > 3 & 1 == 1 # See if the bitmask contains the 2^0 permission
    => true
    
    > 3 & 4 == 4 # See if the bitmask contains the 2^2 permission
    => false
    

提交回复
热议问题