Subscribe to PHP Freaks RSS

Are Bitwise Operators Still Relevant in Modern PHP?

syndicated from on August 21, 2017

Many of you probably scratched your heads reading this title. "Bitwhat?"

In this article, we'll look at what bitwise operators are, and whether or not their use is still relevant in this modern age of computing.

<h2 id=Example Use Case

I'd been trying to really understand bitwise operators for a long time, until an example made it click for me. So that's what we'll do - dive straight into an example.

Imagine you have a website on which a given user can have specific permissions. For example, a magazine like SitePoint:

  • a author can CRUD drafts, and and edit their profile.
  • an editor can, in addition to the above, CRUD drafts and finished posts, and CRUD author profiles.
  • an administrator can, in addition to the above, add administrator permissions.

Since a user can have multiple permissions, there are several ways of defining permissions in a database and the system using it.

The Double Join

Add roles, add permissions, attach permissions to roles in a join table, then create another join table and bind some roles to some users.

This approach creates four extra tables:

  • permissions
  • roles
  • permissions<->roles
  • roles<->users

Quite a bit of overhead. Imagine having to edit these or list them in the app regularly in some frequently visited lists. Only heavy caching would save this app from collapsing under heavy load.

One advantage, though, is that by defining roles really well with intricate permissions, you only have to stick users into roles and you're good - it keeps that join table light and fast.

The Single Join

Add permissions, add a join table, attach some permissions to some users

This approach creates two extra tables:

  • permissions
  • permissions<->users

Much less overhead than the previous example, but you have many more entries in the join table because a user can have a LOT of permissions (just the CRUD for drafting is 4 permissions on its own). With a lot of users and a lot of permissions, this table can get heavy quickly.

The Column Stampede

Add a column into the users table for each permission, then make its datatype a tinyint(1) (basically a boolean) to check the permission as "on" or "off".

Setting permissions for a user would then look something like this:

UPDATE `users` SET `editProfile` = 1, `deleteProfile` = 0, `createDraft` = 1, `publishDraft` = 0 ... WHERE `id` = 5

This approach adds no extra tables, but needlessly expands the table into gargantuan width, and requires a modification of the database every time a new permission is added. It's a fine approach for when you know you'll have at most two or three permissions for the foreseeable future, but shouldn't be used for anything more than that.

However, because the list of columns, when looked at from afar, resembles a binary number (1010), this approach is an excellent segway into another...

The Bitwise Approach

Before we dive deeper into this approach, let's have a crash course in binary.

Binary Numbers

All computers store data as binary: 0 or 1. So, the number 14 is actually stored as: 1110. How so?

Binary numbers are evaluated from right to left when calculating their value, just like real numbers. So the number 1337 means:

  • 1 x 7
    • 3 x 10
    • 3 x 100
    • 1 x 1000

Because each digit in the decimal system (base 10) gets multiplied by 10. The first one is 1, the next one is 10, the next after that 100, the next 1000, etc.

Continue reading %Are Bitwise Operators Still Relevant in Modern PHP?%