Bitwise Operators PHP

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. lists PHP's bitwise operators.

See 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 is 0, so a 0 is put in this position of the result.

Bitwise operators are very useful in C, from which PHP takes inspiration, but you rarely will need to use them in a PHP script.

Bitwise Operators

Bitwise-AND-of-12-and-10


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics