Recently, while working on a project that incorporates a custom Content Management System (CMS), I came across building the complex system of Role-Based Access Control (RBAC). The core base of these Roles were the permissions of various types i.e. create, modify, remove, read-only etc.
The very first method that came to my mind to build this system was from a book I read a couple of years ago titled as PHP and MySQL Create Modify Reuse published by Wrox Press. This method included the clever use of Bitwise Operations to assign one or more permissions to a Role. This was probably the first time when I was interested in Binary system of computing.
Since I had more than 12 types of permissions to assign to various Roles, I thought it would be useful to have a list of special integers in hand. So I made following small PHP function and ran it through browser to get the list of special integers.
I thought it might also be helpful for anyone else so I have put it on Github Gist. Please feel free to use it to do wonders.
So what exactly are special integers?
Special integers are special in way because they are power of 2 i.e.
2, 4, 8, 16 and so on. These special integers return amazing outcome due to their Binary structure when they are used with Bitwise operators. By analysing the following list of integers you can see that these integers (1, 2, 4, 8, 16) have only one digit “1” in their structure when converted to Binary.
So basically when used with Bitwise AND and OR operators, they provide very unique results. For example:
Pretty interesting! Hope this was informative. Happy programming!