Clearing Enum Flags
Somebody just pointed out to me that the enum guidelines don’t provide any information on how to clear a flag in a flags enum variable.
This is quite easy to do if the enum has a value (member) that has all the flags set. Such value is usually called All.
[Flags]
public enum Foos {
A = 1,
B = 2,
C = 4,
D = 8,
AB = A | B,
CD = C | D,
All = AB | CD
}
static class Program {
static void Main() {
Foos value = Foos.AB;
Console.WriteLine(ClearFlag(value,Foos.A);
}
public static Foos ClearFlag(Foos value, Foos flag) {
value = value & (Foos.All ^ flag);
return value;
}
}
If the enum does not contain the All value, you can manually create it by combining existing flags.
public static Foos ClearFlag(Foos value, Foos flag) {
value = value & ((Foos.AB|Foos.CD) ^ flag);
return value;
}
Or you can manufactore the All value from UInt32.
public static Foos ClearFlag(Foos value, Foos flag) {
Foos all;
unchecked {
all = (Foos)UInt32.MaxValue;
}
value = value & (all ^ flag);
return value;
}
Keep in mind that not all enums are backed with 32-bit integers. For these that are larger, for example enums with ulong as the underlying type, you will need to use UINt32.MaxValue instead.
[Flags]
public enum Foos : ulong {
…
}
public static Foos ClearFlag(Foos value, Foos flag) {
Foos all;
unchecked {
all = (Foos)UInt64.MaxValue;
}
value = value & (all ^ flag);
return value;
}
What’s interesting is that you don’t actually need to change anything if the underlying type is smaller, i.e. you can cast UInt64 to an enum backed with a byte. The cast operator takes care of the size mismatch. This means you could always use UInt64. The only drawback would be that it makes the variable all larger than it has to be, which is a slight inefficiency, probably not detectable in all but the most targeted benchmarks.
Recent Posts
- Understanding API First Strategy and Benefits
- Frankenstein APIs Explained! - API Cyber Security Series
- API Security 101 - Cyber Security Explained
- API Trends 2022 - API Security and Cybersecurity
- API Trends 2022 - Seamless Integration Solutions
- API Trends 2022 - Adaptive API Management
- API Trends 2022 - API Integration Automation
- API Trends 2022 - Industry Specific Breakouts
- API Trends 2022 - API Best Practices
- API Trends 2022 - Open API Standards
- API Trends 2022 - API Integration Experience
- API Trends 2022 - API-Led Modernization
- API Trends 2022 - API Economy Growth
- Brenton House - Give your App and APIs a Turbo Boost – Part 2
- Easily Enable Speech Recognition in Titanium iOS using Hyperloop
Tag Cloud
- .NET
- adaptive api management
- android
- api
- api automation
- api best practices
- api economy
- api experience
- api first
- api integration
- api integrations
- api management
- api security
- api standards
- api strategy
- api trends
- apis
- appcelerator
- automation
- boot
- Brenton House
- C#
- Cisco VPN
- Cloud
- Community News
- Continuous Integration
- crash
- cybersecurity
- Dropbox
- fhir apis
- Free Stuff
- General Software Development
- Google Drive
- graphql
- healthcare apis
- houserules
- hyperloop
- ios
- json schema
- Live Mesh
- Mac OS X
- Microsoft
- mobile
- mobile api
- native
- open banking
- openapi
- Other Stuff
- SkyDrive
- speech
- Stuff
- swagger
- Team Systems
- titanium native
- titanium turbo
- Unit Testing
- Vista
- Visual Studio
- Visual Studio 11
- voice
- win7
- Windows 7
- Windows 8
- windows update