locked
How do you Un-Set a bit field (flag enumeration)...? RRS feed

  • Question

  • Ok, we all know how to SET an enumeration with "OR" and we all know how to check an enumeration with "AND", but g.d. how do you clear (unset, remove) a flag?

    <Flags()> _

    Public Enum LineOutputOptions

    TraceLevel = 2

    MemberPosition = 4

    TreeifyOutput = 8

    SourceTypeName = 16

    End Enum

    Monday, August 28, 2006 8:50 PM

Answers

  • Use the Xor operator (^ in C#):

    Dim options As LineOutputOptions = LineOutputOptions.MemberPosition Or LineOutputOptions.SourceTypeName Or LineOutputOptions.TraceLevel
    options = options
    Xor LineOutputOptions.SourceTypeName

    Andrej

    Monday, August 28, 2006 9:44 PM

All replies

  • Use the Xor operator (^ in C#):

    Dim options As LineOutputOptions = LineOutputOptions.MemberPosition Or LineOutputOptions.SourceTypeName Or LineOutputOptions.TraceLevel
    options = options
    Xor LineOutputOptions.SourceTypeName

    Andrej

    Monday, August 28, 2006 9:44 PM
  • This is working only if it is sure that the flag is set. If the state of the bit is unknown, you have to set it first with an "or":

     

    Code Snippet

    options = options Or LineOutputOptions.SourceTypeName Xor LineOutputOptions.SourceTypeName

     

    Tobias

    Monday, May 14, 2007 1:00 PM
  • And it with the one's complement of the bit you're trying to clear.

    In C#:

        _flags &= ~LineOutputOptions.TraceLevel;

    • Proposed as answer by _Ed Noepel Thursday, November 10, 2011 1:30 PM
    Monday, May 30, 2011 5:54 PM