This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Use Bit Flags with .NET Enumerations

How MATLAB Supports Bit-Wise Operations on System.Enum

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 MATLAB example, NetDocEnum.MyDays enumeration, in the following examples.

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(...

Create a variable allMtgs of all days with meetings:

allMtgs = bitor(teamMtgs,...

To see which days belong to each variable, type:

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(...
allMtgs = bitor(teamMtgs,...
teamMtgs = 
Thursday, Friday

allMtgs = 
Monday, Thursday, Friday

Testing for Membership

Create the following RemindMe function:

function RemindMe(day)
% day = NetDocEnum.MyDays enumeration
teamMtgs = bitor(...
allMtgs = bitor(teamMtgs,...

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

Use the RemindMe function:

today = NetDocEnum.MyDays.Monday;
Meeting today.

See Also

| | |

Related Topics

Was this topic helpful?