Main Content

Use Bit Flags with .NET Enumerations

R2026b

Some .NET enumerations are designed as bit flags, where each enumeration member represents an independent option. Bit flags allow a .NET enumeration to represent combinations of independent options as a single value. To use bit flags, the .NET enumeration must:

  • Include the Flags attribute.

  • Assign each member a value that is a power of 2.

MATLAB® supports bitwise operations on .NET enumeration values. You can use the MATLAB functions bitand, bitnot, bitor, and bitxor to create, combine, and test bit flags.

The following examples use the sample enumeration NetDocEnum.MyDays. For the C# code that defines this enumeration, see NetDocEnum Example Assembly.

Create Bit Flags

You can create a bit flag that represents multiple .NET enumeration members as one value. Use the bitor function to combine .NET enumeration members.

For example, suppose that you have these weekly activities:

  • Monday — Department meeting

  • Wednesday and Friday — Team meeting

  • Thursday — Volleyball night

Combine members of the System.Enum.MyDays enumeration to create MATLAB variables that represent more than one day.

Create a bit flag that represents team meeting days by combining multiple enumeration members into a single flag value.

teamMtgs = bitor( ...
    NetDocEnum.MyDays.Friday, ...
    NetDocEnum.MyDays.Wednesday)
teamMtgs = 
Wednesday, Friday

Create a bit flag for all meeting days by adding another enumeration member (Monday) to an existing flag value.

allMtgs = bitor(teamMtgs, ...
    NetDocEnum.MyDays.Monday)
allMtgs = 
Monday, Wednesday, Friday

Remove a Flag

You can remove a .NET enumeration member from a bit flag by using the bitxor function, provided that the flag is already present. For example, if the Wednesday meeting is canceled this week,remove the specific enumeration member (Wednesday) from the flag value.

thisWeekMtgs = bitxor(allMtgs,NetDocEnum.MyDays.Wednesday)
thisWeekMtgs = 
Monday, Friday

Bitwise operations do not modify the original variable. In this example, allMtgs remains unchanged.

Replace a Flag

You can change which .NET enumeration members are included in a bit flag by using multiple bitwise operations. For example, to permanently move the team meeting from Wednesday to Thursday, first remove the existing enumeration Wednesday.

teamMtgs = (bitand( ...
    teamMtgs, ...
    bitnot(NetDocEnum.MyDays.Wednesday));

Add a different enumeration member, Thursday.

teamMtgs = bitor(teamMtgs,NetDocEnum.MyDays.Thursday)
teamMtgs = 
Thursday, Friday

Rebuild the combined flag value using the updated members.

allMtgs = bitor(teamMtgs,NetDocEnum.MyDays.Monday)
allMtgs = 
Monday, Thursday, Friday

Test for Membership

You can test whether a bit flag includes a specific .NET enumeration member by using bitwise operations. For example, create a function that determines whether the input day belongs to the team meeting days or to all meeting days.

function RemindMe(day)
% day = NetDocEnum.MyDays enumeration
teamMtgs = bitor( ...
    NetDocEnum.MyDays.Friday, ...
    NetDocEnum.MyDays.Wednesday);
allMtgs = bitor(teamMtgs, ...
    NetDocEnum.MyDays.Monday);

% Check whether the input day is included in the team meeting days
if eq(day,bitand(day,teamMtgs))
    disp("Team meeting today.")
elseif eq(day,bitand(day,allMtgs))
    disp("Department meeting today.")    
else
    disp("No meetings today!")
end
end

Use the RemindMe function to determine which, if any, meeting you have on Monday.

RemindMe(NetDocEnum.MyDays.Monday)
Department meeting today.

NetDocEnum Example Assembly

This C# code defines the .NET enumerations used on this page. Save this code in a file named NetDocEnum.cs. To run the examples, build and load the NetDocEnum assembly as described in Build and Load .NET Assembly for MATLAB.

{
    [System.Flags()]
    public enum MyDays
    {
        None = 0,
        Monday = 1,
        Tuesday = 2,
        Wednesday = 4,
        Thursday = 8,
        Friday = 16,
        Saturday = 32,
        Sunday = 64
    }

    public enum Range : long
    {
        Max = 2147483648L,
        Min = 255L
    }
}

View the .NET enumeration classes.

asm.Enums
ans = 
    'NetDocEnum.MyDays'
    'NetDocEnum.Range'

Display the underlying types.

today = NetDocEnum.MyDays.Friday;
System.Enum.GetUnderlyingType(today.GetType).FullName
ans = 
System.Int32
maxValue = NetDocEnum.Range.Max;
System.Enum.GetUnderlyingType(maxValue.GetType).FullName
ans = 
System.Int64

See Also

| | |

Topics