PHP/Operator/Bitwise Operators

Материал из Web эксперт
Перейти к: навигация, поиск

Bitwise Operators

 
Example           Operation              Result 
$a & $b           And                    Bits that are set in both $a and $bare set. 
$a | $b           Or                     Bits that are set in either $a or $bare set. 
$a ^ $b           Xor                    Bits that are set in $$aor $b but not in both are set. 
~ $a              Not                    Bits that are set in $a are not set, and vice versa. 
$a << $b          Shift left             Shift the bits of $ato the left $b steps. 
$a >> $b          Shift right            Shift the bits of $ato the right $b steps.



Bitwise operators in action

<?php
     $answers = 88;
     $question_four = 8;
   
     $answers = $answers ^ $question_four;
     print($answers . "<br />");
     $answers = $answers | $question_four;
     print($answers . "<br />");
     $answers = $answers ^ $question_four;
     print($answers . "<br />");
?>



Bitwise Operators summary table

 
Operation         Operator    Class     Associativity    Example
Bitwise AND       &           Binary    Left             $a & $b
Bitwise           |           Binary    Left             $a | $b
inclusive OR
Bitwise exclusive ^           Binary    Left             $a ^ $b
OR (XOR)
Bitwise NOT       ~           Unary     Right            ~$a
Bitwise           <<          Binary    Left             $a << $b
left-shift
Bitwise           >>          Binary    Left             $a >> $b 
right-shift