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 00001 0000 00012 0000 00103 0000 00114 0000 0100
5 0000 01016 0000 01107 0000 01118 0000 10009 0000 1001
10 0000 101011 0000 101112 0000 110013 0000 110114 0000 1110
14 0000 111116 0001 000017 0001 000118 0001 001019 0001 0011
20 0001 010021 0001 010122 0001 011023 0001 011124 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.