What is a bitmask and a mask?

Bits and Bytes In computing, numbers are internally represented in binary. This means, where you use an integer type for a variable, this will actually be represented internally as a summation of zeros and ones. As you might know, a single bit represents one 0 or one 1. A concatenation of eight of those bits … Read more

When is it better to store flags as a bitmask rather than using an associative table?

Splendid question! Firstly, let’s make some assumptions about “better”. I’m assuming you don’t much care about disk space – a bitmask is efficient from a space point of view, but I’m not sure that matters much if you’re using SQL server. I’m assuming you do care about speed. A bitmask can be very fast when … Read more

How can I use a bitmask?

Briefly, a bitmask helps to manipulate the position of multiple values. There is a good example here; Bitflags are a method of storing multiple values, which are not mutually exclusive, in one variable. You’ve probably seen them before. Each flag is a bit position which can be set on or off. You then have a … Read more

Declaring and checking/comparing (bitmask-)enums in Objective-C

Declaring Bitmasks: Alternatively to assigning absolute values (1, 2, 4, …) you can declare bitmasks (how these are called) like this: typedef enum : NSUInteger { FileNotDownloaded = (1 << 0), // => 00000001 FileDownloading = (1 << 1), // => 00000010 FileDownloaded = (1 << 2) // => 00000100 } DownloadViewStatus; or using modern … Read more

How to implement a bitmask in php?

It’s quite simple actually. First a bit of code to demonstrate how it can be implemented. If you don’t understand anything about what this code is doing or how it works, feel free to ask additional questions in the comments: const FLAG_1 = 0b0001; // 1 const FLAG_2 = 0b0010; // 2 const FLAG_3 = … Read more

What to do when bit mask (flags) enum gets too large

I see values from at least a handful of different enumerations in there… My first thought was to approach the problem by splitting the permissions up in logical groups (RuleGroupPermissions, RulePermissions, LocationPermissions, …), and then having a class (WebAgentPermissions) exposing a property for each permission enum type. Since the permission values seem repetitive, you could … Read more

Using a bitmask in C#

The traditional way to do this is to use the Flags attribute on an enum: [Flags] public enum Names { None = 0, Susan = 1, Bob = 2, Karen = 4 } Then you’d check for a particular name as follows: Names names = Names.Susan | Names.Bob; // evaluates to true bool susanIsIncluded = … Read more