How to remove an item for a OR'd enum?

后端 未结 7 1735
后悔当初
后悔当初 2020-12-22 18:17

I have an enum like:

public enum Blah
{
    RED = 2,
    BLUE = 4,
    GREEN = 8,
    YELLOW = 16
}

Blah colors = Blah.RED | Blah.BLUE | Blah.YELLOW;
         


        
7条回答
  •  一生所求
    2020-12-22 18:44

    What about xor(^)?

    Given that the FLAG you are trying to remove is there, it will work.. if not, you will have to use an &.

    public enum Colour
    {
        None = 0,  // default value
        RED = 2,
        BLUE = 4,
        GREEN = 8,
        YELLOW = 16,
        Orange = 18  // Combined value of RED and YELLOW
    }
    
    colors = (colors ^ Colour.RED) & colors;
    

提交回复
热议问题