Bitwise Operators

A binary digit, which may be 1 or 0, is called a bit. Bitwise operators are similar to logical operators, but where logical operators work on TRUE and FALSE, bitwise operators view numbers from a binary perspective. When using logical operators, 1 and 10 are both TRUE, but to a bitwise operator 1 looks like 0001 and 10 looks like 1010. A logical AND of 1 and 10 results in TRUE. A bitwise AND of 1 and 10 results in 0. This is because each bit of the two numbers is compared by a bitwise AND. Table 2.7 lists PHP's bitwise operators.

See Table 2.8 for an example of a bitwise operation, which shows that (12 & 10) == 8. Matching bits are operated on. In the rightmost position 0 and 0 are operated on with a bitwise AND. The result ...

Get Core PHP Programming: Using PHP to Build Dynamic Web Sites 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.