Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining which flags are missing from enum

Tags:

c#

.net

Lets say i have the enum below:

[Flags]
public enum NotifyType
{

    None = 0,
    Window = 1 << 0,
    Sound = 1 << 1,
    Flash = 1 << 2,
    MobileSound = 1 << 3,
    MobilePush = 1 << 4
}

Considering two enums:

var myenums = Window | Sound | Flash;


//var possibleUpdate = Window | MobilePush;

void UpdateMyEnums(NotifyType possibleUpdate)
{
    //Does myenums contain all the flags in 'possibleUpdate'?  If not add
    //the missing flags to myenums

}

How is it possible to determine that the myenums variable does not contain the NotifyType.MobilePush value in comparison to the possibleUpdate? Do i have to test each flag in possibleUpdate against myenums?

I am using C# on .NET 4.0

like image 884
Mike_G Avatar asked Dec 06 '25 03:12

Mike_G


1 Answers

if (myenums & possibleUpdate != possibleUpdate)
    //not a possible update

To get the flags needed not in myenums:

NotifyType missing = (~(myenums ^ wanted) ^ wanted) & (myenums | wanted);
like image 167
It'sNotALie. Avatar answered Dec 08 '25 18:12

It'sNotALie.