PHP Bitwise Operators

Bitwise operators examine and manipulate integer values on the level of individual bits that make up the integer value (thus the name). To fully understand this concept,you need at least an introductory knowledge of the binary representation ofdecimal integers.

Decimal Integer Binary Representation
2 10
5 101
10 1010
12 1100
145 10010001
1,452,012 101100010011111101100

The bitwise operators listed in following Table are variations on some of the logical operators but can result in drastically different outcomes.


Example Label Outcome
$a & $b AND And together each bit contained in $a and $b
$a | $b OR Or together each bit contained in $a and $b
$a ^ $b XOR Exclusive-or together each bit contained in $a and $b
~ $b NOT Negate each bit in $b
\$a << \$b Shift left $a will receive the value of $b shifted left two bits
$a >> $b Shift right $a will receive the value of $b shifted right two bits




Content