Using Bitwise operators on flags

后端 未结 9 1930
生来不讨喜
生来不讨喜 2020-12-23 12:11

I have four flags

Current = 0x1  
Past = 0x2  
Future = 0x4  
All = 0x7

Say I receive the two flags Past and Future (setFlags(PAST

9条回答
  •  清歌不尽
    2020-12-23 12:41

    You may also want to add an extension method like this

      enum states {
         Current = 0x1,
         Past = 0x2,
         Future = 0x4,
         All = 0x7
      };
    
      static bool Is(this states current, states value) {
         return (current & value) == value;
      }
    

    then you can do:

     if(state.Is(states.Past)) {
        // Past
     }
    

提交回复
热议问题