Home > Backend Development > C++ > How to Perform Bitwise Operations on C# [Flags] Enums?

How to Perform Bitwise Operations on C# [Flags] Enums?

Patricia Arquette
Release: 2025-01-13 11:37:44
Original
716 people have browsed it

How to Perform Bitwise Operations on C# [Flags] Enums?

Use C# [Flags] enumeration to perform bit operations

Enumerations with the [Flags] attribute allow manipulation of individual bits in the underlying integer representation. This enables succinct representation of bit masks, where each bit represents a specific flag or option.

Set bit

To set a bit in the [Flags] enumeration, use the bitwise OR operator (|):

<code class="language-csharp">flags = flags | FlagsEnum.Bit4; // 设置位 4</code>
Copy after login

Test position

To test whether a bit is set, use the bitwise AND operator (&):

<code class="language-csharp">bool isBit4Set = (flags & FlagsEnum.Bit4) != 0;</code>
Copy after login

Toggle position

To toggle a bit (set if not set, clear if set), use the bitwise XOR operator (^):

<code class="language-csharp">flags = flags ^ FlagsEnum.Bit4; // 切换位 4</code>
Copy after login

Clear bit

To clear bits, use the bitwise AND operator (&) and the complement of the bit mask:

<code class="language-csharp">flags = flags & ~FlagsEnum.Bit4; // 清除位 4</code>
Copy after login

Custom extension method

For convenience, the following extension methods can be defined to simplify these operations:

<code class="language-csharp">public static bool Has<T>(this System.Enum type, T value) {
    return (((int)(object)type & (int)(object)value) == (int)(object)value);
}

public static bool Is<T>(this System.Enum type, T value) {
    return (int)(object)type == (int)(object)value;
}

public static T Add<T>(this System.Enum type, T value) {
    return (T)(object)(((int)(object)type | (int)(object)value));
}

public static T Remove<T>(this System.Enum type, T value) {
    return (T)(object)(((int)(object)type & ~(int)(object)value));
}</code>
Copy after login

These extension methods can be used as follows:

<code class="language-csharp">SomeType value = SomeType.Grapes;
bool isGrapes = value.Is(SomeType.Grapes); // true
bool hasGrapes = value.Has(SomeType.Grapes); // true

value = value.Add(SomeType.Oranges);
value = value.Add(SomeType.Apples);
value = value.Remove(SomeType.Grapes);

bool hasOranges = value.Has(SomeType.Oranges); // true
bool isApples = value.Is(SomeType.Apples); // false
bool hasGrapes = value.Has(SomeType.Grapes); // false</code>
Copy after login

The above is the detailed content of How to Perform Bitwise Operations on C# [Flags] Enums?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template