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

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

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

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

Bitmask in PHP for settings?

Bit fields are a very handy and efficient tool for dealing with flags or any set of boolean values in general. To understand them you first need to know how binary numbers work. After that you should check out the manual entries on bitwise operators and make sure you know how a bitwise AND, OR … Read more