Removing enum flags

后端 未结 1 707
滥情空心
滥情空心 2021-01-27 12:04

I\'m a little bit puzzled by the removal of enum flags to be perfectly honest.

Let me make an example, let\'s say we have an enum that looks like this

[F         


        
相关标签:
1条回答
  • 2021-01-27 12:50

    Yes, the problem with the XOR operator is that unless you know that you've got all the rest of the flags, it will just flip them. So your XOR operation isn't "remove all but C" - it's "toggle the values of A and B". (So if the input was "A,C" you'd end up with "B,C", for example.)

    & is used because it's masking. The basic idea is that you get a value which contains just the bits you want, and then a bitwise AND of that value with your input value masks it.

    To remove one specific flag (rather than retaining that specific flag), you'd typically use the ~ operator to create a mask of "all but that flag". For example:

    var mask = ~Letter.A;
    var newValue = originalValue & mask; // All previous values other than A
    
    0 讨论(0)
提交回复
热议问题