📜  位掩码 c# 代码示例

📅  最后修改于: 2022-03-11 14:49:01.880000             🧑  作者: Mango

代码示例1
[Flags]
public enum Days
{
    None = 0,    //must have a specified 0
    Sunday = 1 << 0,    //1
    Monday = 1 << 1,    //2
    Tuesday = 1 << 2,    //4
    Wednesday = 1 << 3,    //8
    Thursday = 1 << 4,    //16
    Friday = 1 << 5,    //32
    Saturday = 1 << 6,    //64
    
    Workdays = Monday | Tuesday | Wednesday | Thursday | Friday,    // 0111110
    Vacationdays = Saturday | Sunday,                                // 1000001
    AllDays = Workdays | Vacationdays                                // 1111111
}

//Example:
public Days myDays = Days.Wednesday | Days.Friday;    //myDays = 0101000
public bool containsTuesday = (myDays & Days.Tuesday) == Days.Tuesday ? true : false; // 0101000 & 0000100 = 0000000 (not 0000100, so expression is FALSE)
Console.WriteLine(containsTuesday); //output: false

//Operator cheat-sheet:
// & - apply mask
// | - combine
// ^ - combine/toggle, get difference