2.3. Working with Permissions and Bitwise Operators
A basic understanding of bit operators and how computers store information is all it takes to understand how our permission scheme will work. The operators I'll focus on are the bitwise-and (&) and bitwise-or (|) operators.
Think of a standard light switch; the circuit can have two states. It's either closed with electricity flowing through it or open so no charge can flow. Each byte that makes up a number stored in a computer is ultimately nothing more than a series of tiny electrical switches, with on (closed) and off (open) values. The computer interprets a series of the consecutive on/off values into something interesting for us humans, such as a letter or a number.
Bitwise operators such as bitwise-and and bitwise-or can compare specific bits in the sequences and flip them on or off. If a sequence of switches or bits changes, then the meaning does as well. It's customary to represent the on state with the number 1 and off with 0. Take a brief look at how the following numbers 0 through 24 are represented:
0 0000 0000 | 1 0000 0001 | 2 0000 0010 | 3 0000 0011 | 4 0000 0100 |
5 0000 0101 | 6 0000 0110 | 7 0000 0111 | 8 0000 1000 | 9 0000 1001 |
10 0000 1010 | 11 0000 1011 | 12 0000 1100 | 13 0000 1101 | 14 0000 1110 |
14 0000 1111 | 16 0001 0000 | 17 0001 0001 | 18 0001 0010 | 19 0001 0011 |
20 0001 0100 | 21 0001 0101 | 22 0001 0110 | 23 0001 0111 | 24 0001 1000 |
Notice there is something special about the numbers 1, 2, 4, 8 and 16. In each of these values the left-most 1-bit advances a placeholder. ...
Get PHP and MySQL®: Create-Modify-Reuse now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.