The bitwise operators are used to evaluate variables value on a bit level, or in other words they are used to compare variables bit by bit.

The bitwise operators are:

  • AND (and)
  • OR (or)
  • XOR (exclusive or)
  • NOT (and not)
  • SHIFT LEFT (shift bits left)
  • SHIFT RIGHT (shift bits right)

The example below shows certain ways of using bitwise operators.

Example with bitwise operators

$a & $b; // (AND) Bits set in both variables will be stored as the result
$a | $b; // (OR) Bits set in either variable will be stored as the result
$a ^ $b; // (XOR) Bits set in $a or $b variable but not in bothw ill be stored as the result
~ $a;    // (NOT) Bits not set in $a variable will be stored as the result
$a << $b; // (SHIFT LEFT) Shifts bits in variable $a for $b number of steps to the left, filling empty slots with zeros (each steps means 'multiply by 2')
$a>>< $b; // (SHIFT RIGHT) Shifts bits in variable $a for $b number of steps to the right, filling empty slots with zeros (each steps means 'divide by 2')

 

›› go to examples ››