Use Bit Flags with .NET Enumerations

Overview

Many .NET languages support bit-wise operations on enumerations defined with the System.Flags attribute. The MATLAB® language does not have equivalent operations, and, therefore, provides instance methods for performing bit-wise operations on an enumeration object. The bit-wise methods are bitand, bitnot, bitor, and bitxor.

An enumeration can define a bit flag. A bit flag lets you create instances of an enumeration to store combinations of values defined by the members. For example, files and folders have attributes, such as Archive, Hidden, and ReadOnly. For a given file, perform an operation based on one or more of these attributes. With bit-wise operators, you can create and test for combinations.

To use bit-wise operators, the enumeration must have:

  • The Flags attribute. In Framework Version 4, these enumerations also have the HasFlag method.

  • Values that correspond to powers of 2.

Creating .NET Enumeration Bit Flags

Use the NetDocEnum.MyDays enumeration in the following examples. For more information, see Using the NetDocEnum Example Assembly.

Suppose that you have the following scheduled activities:

  • Monday — Department meeting at 10:00

  • Wednesday and Friday — Team meeting at 2:00

  • Thursday — Volley ball night

You can combine members of the MyDays enumeration to create MATLAB variables using the bitor method, which joins two members. For example, to create a variable teamMtgs of team meeting days, type:

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

Create a variable allMtgs of all days with meetings:

allMtgs = bitor(teamMtgs,...
    NetDocEnum.MyDays.Monday);

To see which days belong to each variable, type:

teamMtgs
allMtgs
teamMtgs = 
Wednesday, Friday

allMtgs = 
Monday, Wednesday, Friday

Removing a Flag from a Variable

Suppose that your manager cancels the Wednesday meeting this week. To remove Wednesday from the allMtgs variable, use the bitxor method.

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

Using a bit-wise method such as bitxor on allMtgs does not modify the value of allMtgs. This example creates a variable, thisWeekMtgs, which contains the result of the operation.

Replacing a Flag in a Variable

Suppose that you change the team meeting permanently from Wednesday to Thursday. To remove Wednesday, use bitxor, and use bitor to add Thursday. Since this is a permanent change, update the teamMtgs and allMtgs variables.

teamMtgs = bitor(...
    (bitand(teamMtgs,...
        bitnot(NetDocEnum.MyDays.Wednesday))),...
    NetDocEnum.MyDays.Thursday);
allMtgs = bitor(teamMtgs,...
    NetDocEnum.MyDays.Monday);
teamMtgs
allMtgs
teamMtgs = 
Thursday, Friday

allMtgs = 
Monday, Thursday, Friday

Testing for Membership

Create the following RemindMe function:

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

if eq(day,bitand(day,teamMtgs))
    disp('Team meeting today.')
elseif eq(day,bitand(day,allMtgs))
    disp('Meeting today.')    
else
    disp('No meetings today!')
end
end

Use the RemindMe function:

today = NetDocEnum.MyDays.Monday;
RemindMe(today)
Meeting today.
Was this topic helpful?